由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至关重要!
作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默认参数下是对表进行直方图信息收集,包含该表的自身-表的行数、数据块数、行长等信息;列的分析--列值的重复数、列上的空值、数据在列上的分布情况;索引的分析-索引页块的数量、索引的深度、索引聚合因子).
DBMS_STATS.GATHER_TABLE_STATS的语法如下: DBMS_STATS.GATHER_TABLE_STATS ( ownname VARCHAR2, tabname VARCHAR2, partname VARCHAR2, estimate_percent NUMBER, block_sample BOOLEAN, method_opt VARCHAR2, degree NUMBER, granularity VARCHAR2, cascade BOOLEAN, stattab VARCHAR2, statid VARCHAR2, statown VARCHAR2, no_invalidate BOOLEAN, force BOOLEAN); 参数说明: ownname:要分析表的拥有者 tabname:要分析的表名. partname:分区的名字,只对分区表或分区索引有用. estimate_percent:采样行的百分比,取值范围[0.000001,100],null为全部分析,不采样. 常量:DBMS_STATS.AUTO_SAMPLE_SIZE是默认值,由oracle决定最佳取采样值. block_sapmple:是否用块采样代替行采样. method_opt:决定histograms信息是怎样被统计的.method_opt的取值如下(默认值为FOR ALL COLUMNS SIZE AUTO): for all columns:统计所有列的histograms. for all indexed columns:统计所有indexed列的histograms. for all hidden columns:统计你看不到列的histograms for columns <list> SIZE <N> | REPEAT | AUTO | SKEWONLY:统计指定列的histograms.N的取值范围[1,254]; REPEAT上次统计过的histograms;AUTO由oracle决定N的大小;SKEWONLY multiple end-points with the same value which is what we define by "there is skew in thedata degree:决定并行度.默认值为null. granularity:Granularity of statistics to collect ,only pertinent if the table is partitioned. cascade:是收集索引的信息.默认为FALSE. stattab:指定要存储统计信息的表,statid如果多个表的统计信息存储在同一个stattab中用于进行区分.statown存储统计信息表的拥有者.以上三个参数若不指定,统计信息会直接更新到数据字典. no_invalidate: Does not invalidate the dependent cursors if set to TRUE. The procedure invalidates the dependent cursors immediately if set to FALSE. force:即使表锁住了也收集统计信息. 例子: execute dbms_stats.gather_table_stats(ownname => 'owner',tabname => 'table_name' ,estimate_percent => null ,method_opt => 'for all indexed columns' ,cascade => true); ------------------------------------------------------------------------------------------------------------------------
自从Oracle8.1.5引入dbms_stats包,Experts们便推荐使用dbms_stats取代analyze。 理由如下
如何使用dbms_stats分析统计信息? sql> exec dbms_stats.create_stat_table(ownname => 'scott',stattab => 'stat_table') ;
sql> exec dbms_stats.export_schema_stats(ownname => 'scott',stattab => 'stat_table') ;
Exec dbms_stats.gather_schema_stats(
sql> exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'work_list',estimate_percent => 10,method_opt=> 'for all indexed columns') ;
SQL> exec dbms_stats.gather_index_stats(ownname => 'crm2',indname => 'IDX_ADM_PERMISSION_PID_MID',estimate_percent => '10',degree => '4') ;
SQL>dbms_stats.delete_table_stats(ownname => 'scott',tabname => 'work_list') ;
sql> exec dbms_stats.import_table_stats(ownname => 'scott',tabname => 'work_list',stattab => 'stat_table') ;
sql> exec dbms_stats.import_schema_stats(ownname => 'scott',stattab => 'stat_table');
SQL> exec dbms_stats.import_index_stats(ownname => 'crm2',indname => 'IDX_ADM_PERMISSION_PID_MID',stattab => 'stat_table')
SQL> select table_name,num_rows,a.blocks,a.last_analyzed from all_tables a where a.table_name='WORK_LIST';
可以查看表 DBA_TABLES来查看表是否与被分析过,如: SELECT TABLE_NAME, LAST_ANALYZED FROM DBA_TABLES
这是对命令与工具包的一些总结 (责任编辑:IT) |