直到今天,仍然有很多的朋友在收集统计信息的时候仍然喜欢使用analyze命令。当然,使用这种方式的可能都是一些比较资深的DBA,他们从Oracle 8i/9i时代就开始使用这个命令。
Oracle 10g的官方文档中以及明确提出,不要使用analyze命令收集统计信息。
Do not use the COMPUTE and ESTIMATE clauses of ANALYZE to collect optimizer statistics. These clauses are supported for backward compatibility. Instead, use the DBMS_STATS package, which lets you collect statistics in parallel, collect global statistics for partitioned objects, and fine tune your statistics collection in other ways. The optimizer, which depends upon statistics, will eventually use only statistics that have been collected by DBMS_STATS.
除了上面提到的,不能使用并行之外,analyze命令可能还会导致一些错误的结果。
下面以Oracle 11.2.0.3为例,看看两种方式收集到的不同结果。
通过上面的例子可以看到,ANALYZE和DBMS_STATS收集叶块上的不同结果,这有可能导致优化器选择不同的执行计划。
Oracle 10g的官方文档中以及明确提出,不要使用analyze命令收集统计信息。
Do not use the COMPUTE and ESTIMATE clauses of ANALYZE to collect optimizer statistics. These clauses are supported for backward compatibility. Instead, use the DBMS_STATS package, which lets you collect statistics in parallel, collect global statistics for partitioned objects, and fine tune your statistics collection in other ways. The optimizer, which depends upon statistics, will eventually use only statistics that have been collected by DBMS_STATS.
除了上面提到的,不能使用并行之外,analyze命令可能还会导致一些错误的结果。
下面以Oracle 11.2.0.3为例,看看两种方式收集到的不同结果。
SQL> create table milltec as select rownum id, object_name from dba_objects;
SQL> create index idx_milltec on milltec(id);
SQL> exec dbms_stats.gather_table_stats('SYS','MILLTEC');SQL> select OWNER, INDEX_NAME, LEAF_BLOCKS, LAST_ANALYZED from DBA_INDEXESwhere OWNER = 'SYS' and INDEX_NAME = 'IDX_MILLTEC';OWNER INDEX_NAME LEAF_BLOCKS LAST_ANALYZED
------------------------------ ------------------------------ ----------- -------------------
SYS IDX_MILLTEC 193 2015-06-21 07:16:39SQL> analyze index IDX_MILLTEC compute statistics;SQL> select OWNER, INDEX_NAME, LEAF_BLOCKS, LAST_ANALYZED from DBA_INDEXESwhere OWNER = 'SYS' and INDEX_NAME = 'IDX_MILLTEC';OWNER INDEX_NAME LEAF_BLOCKS LAST_ANALYZED
------------------------------ ------------------------------ ----------- -------------------
SYS IDX_MILLTEC 193 2015-06-21 07:17:47SQL> delete from milltec where id > 2000;
SQL> commit;SQL> exec dbms_stats.gather_table_stats('SYS','MILLTEC');SQL> select OWNER, INDEX_NAME, LEAF_BLOCKS, LAST_ANALYZED from DBA_INDEXESwhere OWNER = 'SYS' and INDEX_NAME = 'IDX_MILLTEC';OWNER INDEX_NAME LEAF_BLOCKS LAST_ANALYZED
------------------------------ ------------------------------ ----------- -------------------
SYS IDX_MILLTEC 5 2015-06-21 07:19:00SQL> analyze index IDX_MILLTEC compute statistics;SQL> select OWNER, INDEX_NAME, LEAF_BLOCKS, LAST_ANALYZED from DBA_INDEXESwhere OWNER = 'SYS' and INDEX_NAME = 'IDX_MILLTEC';OWNER INDEX_NAME LEAF_BLOCKS LAST_ANALYZED
------------------------------ ------------------------------ ----------- -------------------
SYS IDX_MILLTEC 193 2015-06-21 07:19:38
通过上面的例子可以看到,ANALYZE和DBMS_STATS收集叶块上的不同结果,这有可能导致优化器选择不同的执行计划。