(Oracle 11g)批量生成move表及rebuild索引语句

12C版本有些参数有变化,Move表支持Online,不过没生产上尝试。

-- 批量生成move表及rebuild索引语句
/* 替换以下属性信息,By Xander.Cui
OWNER(SCHEMA NAME): A01_GOLDEN
TABLE_NAME: 'SFC_SN_TRAC_PART'
TABLESPACE: 沿用现在的表空间(更改请指定)
PARALLEL 4: 开启并行度为4,按照 <= CPU Cores 具体值分配;开启后关闭并行
*/
SELECT '-- Move 表(不含 LONG 字段) CMD:' AS COMMEND FROM DUAL
UNION ALL
SELECT TC.OWNER||'.'||TC.TABLE_NAME||' 表包含 Long 类型字段,不支持 Move,以下 Move 命令请勿执行。'
  FROM DBA_TAB_COLUMNS TC
 WHERE TC.DATA_TYPE = 'LONG'
   AND TC.OWNER = UPPER('A01_GOLDEN')
   AND TC.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
   AND ROWNUM = 1
UNION ALL
-- Move 普通表
SELECT 'ALTER TABLE ' || T.OWNER || '.' || T.TABLE_NAME || ' MOVE TABLESPACE '|| T.TABLESPACE_NAME ||' PARALLEL 4;'
       ||CHR(10)||'ALTER TABLE ' || T.OWNER || '.' || T.TABLE_NAME || ' NOPARALLEL;'
  FROM DBA_TABLES T
 WHERE T.OWNER = UPPER('A01_GOLDEN')
   AND T.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
   AND T.PARTITIONED = 'NO'
UNION ALL
-- Move 分区表已有分区
SELECT 'ALTER TABLE '||TP.TABLE_OWNER||'.'||TP.TABLE_NAME||' MOVE PARTITION '||TP.PARTITION_NAME||' TABLESPACE '||TP.TABLESPACE_NAME||' PARALLEL 4;'
       ||CHR(10)||'ALTER TABLE '||TP.TABLE_OWNER||'.'||TP.TABLE_NAME||' NOPARALLEL;'
  FROM DBA_TAB_PARTITIONS TP
 WHERE TP.TABLE_OWNER = UPPER('A01_GOLDEN')
   AND TP.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
UNION ALL
SELECT '-- Move 表(不含 LONG 字段) LOB 列(如果有) CMD:' FROM DUAL
UNION ALL
-- Move 普通表 Lob 字段
SELECT 'ALTER TABLE '||A.OWNER||'.'||A.TABLE_NAME||' MOVE LOB('||A.COLUMN_NAME||') STORE AS (TABLESPACE '||A.TABLESPACE_NAME||') PARALLEL 4;'
       ||CHR(10)||'ALTER TABLE '||A.OWNER||'.'||A.TABLE_NAME||' NOPARALLEL;'
  FROM DBA_LOBS A
 WHERE A.PARTITIONED = 'NO'
   AND A.OWNER = UPPER('A01_GOLDEN')
   AND A.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
UNION ALL
-- Move 分区表 Lob 字段
SELECT 'ALTER TABLE '||TP.TABLE_OWNER||'.'||TP.TABLE_NAME||' MOVE PARTITION '||TP.PARTITION_NAME||' LOB('||C.COLUMN_NAME||') STORE AS ('||'TABLESPACE '||TP.TABLESPACE_NAME||') PARALLEL 4;'
       ||CHR(10)||'ALTER TABLE '||TP.TABLE_OWNER||'.'||TP.TABLE_NAME||' NOPARALLEL;'
  FROM DBA_TAB_PARTITIONS TP
  LEFT JOIN DBA_TAB_COLUMNS C
    ON TP.TABLE_OWNER = C.OWNER
   AND TP.TABLE_NAME = C.TABLE_NAME
 WHERE C.DATA_TYPE LIKE '%LOB'
   AND C.OWNER = UPPER('A01_GOLDEN')
   AND TP.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
UNION ALL
SELECT '-- Rebuild 索引(索引名不包含 "$$" 字符串的索引) CMD:' FROM DUAL
UNION ALL
-- Rebuild 非分区索引
SELECT 'ALTER INDEX '||I.OWNER||'.'||I.INDEX_NAME||' REBUILD ONLINE TABLESPACE '||I.TABLESPACE_NAME||' PARALLEL 4;'
       ||CHR(10)|| 'ALTER INDEX ' || I.OWNER || '.' || I.INDEX_NAME ||' NOPARALLEL;'
  FROM DBA_INDEXES I
 WHERE I.STATUS = 'VALID'
   AND I.OWNER = UPPER('A01_GOLDEN')
   AND I.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
   AND I.INDEX_NAME NOT LIKE '%$$%'
   AND I.PARTITIONED = 'NO'
UNION ALL
-- Rebuild 分区索引
SELECT 'ALTER INDEX '||I.OWNER||'.'||I.INDEX_NAME||' REBUILD ONLINE TABLESPACE '||I.TABLESPACE_NAME||' PARALLEL 4;'
       ||CHR(10)|| 'ALTER INDEX ' || I.OWNER || '.' || I.INDEX_NAME ||' NOPARALLEL;'
  FROM DBA_INDEXES I
  LEFT JOIN DBA_IND_PARTITIONS IP
    ON I.OWNER = IP.INDEX_OWNER
   AND I.INDEX_NAME = IP.INDEX_NAME
 WHERE I.OWNER = UPPER('A01_GOLDEN')
   AND I.TABLE_NAME = UPPER('SFC_SN_TRAC_PART')
   AND I.INDEX_NAME NOT LIKE '%$$%'
   AND I.PARTITIONED = 'YES'

写完SQL,记录备用。

你可能感兴趣的:(数据库oraclesql批量)