Oracle进行表分析

有时候Oracle建了索引但是SQL语句执行速度仍旧很慢,可以尝试重新对数据表进行一次分析
有以下集中方法搜索CBO信息

analyze table t1 compute statistics fortable; --针对表收集信息

analyze table t2 compute statistics for allcolumns; --针对表字段收集信息

analyze table t3 compute statistics for all indexescolumns; --收集索引字段信息

analyze table t4 computestatistics;       --收集表,表字段,索引信息

analyze table t5 compute statistics for all indexes;         --收集索引信息

analyze table t6 compute statistics for table for all indexesfor allcolumns;   --

CREATE OR REPLACE PROCEDURE ANALYZEALLTABLE IS
  --分析所有表及索引。便于有效的使用CBO优化器  
BEGIN
  --分析所有表:analyze table TABLENAME compute statistics  
  FOR CUR_ITEM IN (SELECT TABLE_NAME FROM USER_TABLES) LOOP
    BEGIN
      EXECUTE IMMEDIATE 'analyze table ' || CUR_ITEM.TABLE_NAME ||
                        ' compute statistics';
    EXCEPTION
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('分析表异常:' || SQLERRM);
    END;
  END LOOP;
  --分析所有索引:analyze index INDEXNAME estimate statistics  
  FOR CUR_ITEM IN (SELECT INDEX_NAME FROM USER_INDEXES) LOOP
    BEGIN
      EXECUTE IMMEDIATE 'analyze index ' || CUR_ITEM.INDEX_NAME ||
                        ' estimate statistics';
    EXCEPTION
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('分析索引异常:' || SQLERRM);
    END;
  END LOOP;
END ANALYZEALLTABLE;

你可能感兴趣的:(Oracle进行表分析)