分析表空间 索引 表

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system
 
SQL>
SQL> CREATE OR REPLACE PROCEDURE ANALYZE_ALL_TABLES(TS_NAME IN  VARCHAR2)
  2  AS
  3  --2010-07-23 MATTER
  4  --分析所有表及索引。便于有效的使用CBO优化器
  5  BEGIN
  6       --分析所有表:ANALYZE TABLE TABLENAME COMPUTE STATISTICS
  7       FOR CUR_ITEM IN (SELECT A.TABLE_NAME FROM USER_TABLES A WHERE A.TABLESPACE_NAME=TS_NAME) LOOP
  8           BEGIN
  9                EXECUTE IMMEDIATE 'ANALYZE TABLE '|| CUR_ITEM.TABLE_NAME
 10                                 || ' COMPUTE STATISTICS';
 11           EXCEPTION
 12              WHEN OTHERS THEN
 13                  DBMS_OUTPUT.PUT_LINE('分析表异常:'||SQLERRM);
 14           END;
 15       END LOOP;
 16 
 17      --分析所有索引:ANALYZE INDEX INDEXNAME ESTIMATE STATISTICS
 18       FOR CUR_ITEM IN (SELECT A.INDEX_NAME FROM USER_INDEXES A WHERE A.TABLESPACE_NAME=TS_NAME) LOOP
 19           BEGIN
 20                EXECUTE IMMEDIATE 'ANALYZE INDEX '|| CUR_ITEM.INDEX_NAME
 21                                  || ' ESTIMATE STATISTICS';
 22           EXCEPTION
 23              WHEN OTHERS THEN
 24                  DBMS_OUTPUT.PUT_LINE('分析索引异常:'||SQLERRM);
 25           END;
 26       END LOOP;
 27  END ANALYZE_ALL_TABLES;
 28  /
 
Procedure created
 
SQL>
SQL> CREATE OR REPLACE PROCEDURE REBUILD_ALL_INDEXS
  2 
  3  --功能:重建索引。
  4  --说明:如果表空间参数传入NULL,则在原表空间内重建索引,否则在目标表空间重建索引。
  5  --      如果表空间相同,则跳过。
  6  --      ONLY_UNUSABLE表示是否只对无效的索引进行重建
  7  ---作者:81,   2007年6月26日
  8     (TABLESPACE_NAME IN VARCHAR2,--这里是表空间名,如果不改变表空间,可以传入NULL
  9     ONLY_UNUSABLE IN BOOLEAN)    --是否仅对无效的索引操作
 10  AS
 11     SQLT VARCHAR(200);
 12  BEGIN
 13      --只取非临时索引
 14      FOR IDX IN (SELECT A.INDEX_NAME, A.TABLESPACE_NAME, A.STATUS FROM USER_INDEXES A WHERE A.TEMPORARY = 'N' AND A.INDEX_TYPE<>'LOB') LOOP
 15          --如果是如重建无效的索引,且当索引不是无效时,则跳过
 16          IF ONLY_UNUSABLE = TRUE AND IDX.STATUS <> 'UNUSABLE' THEN
 17             GOTO CONTINUE;
 18          END IF;
 19          IF (TABLESPACE_NAME IS NULL) OR IDX.STATUS = 'UNUSABLE' THEN
 20             --如果没有指定表空间,或索引无效,则在原表空间重建
 21             SQLT := 'ALTER INDEX ' || IDX.INDEX_NAME || ' REBUILD ';
 22          ELSIF UPPER(TABLESPACE_NAME) <> IDX.TABLESPACE_NAME THEN
 23             --如果指定的不同的表空间,则在指定表空间待建索引
 24             SQLT := 'ALTER INDEX ' || IDX.INDEX_NAME || ' REBUILD TABLESPACE ' || TABLESPACE_NAME;
 25          ELSE
 26             --如果表空间相同,则跳过
 27             GOTO CONTINUE;
 28          END IF;
 29          DBMS_OUTPUT.PUT_LINE(IDX.INDEX_NAME);
 30          EXECUTE IMMEDIATE SQLT;
 31          <<CONTINUE>>
 32          NULL;
 33       END LOOP;
 34  End REBUILD_ALL_INDEXS;
 35  /
 
Procedure created
 
SQL> exec REBUILD_ALL_INDEXS('BHINDEX',false);
 
PL/SQL procedure successfully completed
 
SQL> exec REBUILD_ALL_INDEXS('BHINDEX',true);
 
PL/SQL procedure successfully completed
 
SQL> exec REBUILD_ALL_INDEXS('ZHINDEX',true);
 
PL/SQL procedure successfully completed
 
SQL> exec REBUILD_ALL_INDEXS('ZHINDEX',false);
 
PL/SQL procedure successfully completed
 
SQL> Exec ANALYZE_ALL_TABLES();
 
begin ANALYZE_ALL_TABLES(); end;

 
SQL> Exec ANALYZE_ALL_TABLES('');
 
PL/SQL procedure successfully completed
 
SQL> Exec ANALYZE_ALL_TABLES('BHDATA');
 
PL/SQL procedure successfully completed
 
SQL> Exec ANALYZE_ALL_TABLES('BHINDEX');
 
PL/SQL procedure successfully completed
 
SQL> Exec ANALYZE_ALL_TABLES('ZHBASIS');
 
PL/SQL procedure successfully completed
 
SQL>  Exec ANALYZE_ALL_TABLES('ZHINDEX');
 
PL/SQL procedure successfully completed
 
SQL> Exec ANALYZE_ALL_TABLES('ZHINDEX');
 
PL/SQL procedure successfully completed
 
SQL>

你可能感兴趣的:(exception,user,table,null,database,statistics)