5、特定列的统计量收集
For all columns是收集所有的数据列,从格式上,method_opt还支持一种特定列统计量的收集格式。具体格式为for columns size xxx [column name]。
SQL> exec dbms_stats.delete_table_stats(user,'T');
PL/SQL procedure successfully completed
SQL> select column_name, num_buckets, histogram from dba_tab_col_statistics where wner='SYS' and table_name='T';
COLUMN_NAMENUM_BUCKETS HISTOGRAM
------------------------------ ----------- ---------------
SQL> exec dbms_stats.gather_table_stats(user,'T',method_opt => 'for columns size 10 owner');
PL/SQL procedure successfully completed
SQL> select column_name, num_buckets, histogram from dba_tab_col_statistics where wner='SYS' and table_name='T';
COLUMN_NAMENUM_BUCKETS HISTOGRAM
------------------------------ ----------- ---------------
OWNER10 HEIGHT BALANCED
这样的格式中,只给指定的列生成指定数量bucket的统计量。那么,如果需要收集多个列的统计量,可以按照两种格式进行指定。
SQL> exec dbms_stats.gather_table_stats(user,'T',method_opt => 'for columns size 10 owner for columns size 5 object_name');
PL/SQL procedure successfully completed
SQL> select column_name, num_buckets, histogram from dba_tab_col_statistics where wner='SYS' and table_name='T';
COLUMN_NAMENUM_BUCKETS HISTOGRAM
------------------------------ ----------- ---------------
OWNER10 HEIGHT BALANCED
OBJECT_NAME5 HEIGHT BALANCED
SQL> exec dbms_stats.delete_table_stats(user,'T');
PL/SQL procedure successfully completed
SQL> exec dbms_stats.gather_table_stats(user,'T',method_opt => 'for columns size 10 owner object_name object_id');
PL/SQL procedure successfully completed
SQL> select column_name, num_buckets, histogram from dba_tab_col_statistics where wner='SYS' and table_name='T';
COLUMN_NAMENUM_BUCKETS HISTOGRAM
------------------------------ ----------- ---------------
OWNER10 HEIGHT BALANCED
OBJECT_NAME10 HEIGHT BALANCED
OBJECT_ID10 HEIGHT BALANCED
6、拓展extended统计量收集
Oracle CBO优化器的统计量统计维度默认是单列。当SQL语句中出现多个条件列的时候,其估算的行数是不准确的。在目前的版本中,有两个方法来提高估算精确度,一个是采用动态采样Dynamic Sampling进行实时收集,另一个就是采用11g的拓展统计量Extended Statistic。
在之前的文章中,我们介绍过如何创建Extended统计量。我们使用method_opt,也可以进行拓展统计量收集。
--Sys用户下
SQL> exec dbms_stats.delete_table_stats(user,'T');
PL/SQL procedure successfully completed
SQL> exec dbms_stats.gather_table_stats(user,'T',method_opt => 'for columns size 10 (owner, object_type)');
begin dbms_stats.gather_table_stats(user,'T',method_opt => 'for columns size 10 (owner, object_type)'); end;
ORA-20000: Unable to create extension: not supported for SYS owned table
ORA-06512: 在 "SYS.DBMS_STATS", line 20337
ORA-06512: 在 "SYS.DBMS_STATS", line 20360
ORA-06512: 在 line 1
转换到scott普通用户下进行试验。
SQL> conn scott/tiger@wilson;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott
SQL> drop table t purge;
Table dropped
SQL> create table t as select * from dba_objects;
Table created
SQL> exec dbms_stats.gather_table_stats(user,'T',method_opt => 'for columns size 10 (owner, object_type)');
PL/SQL procedure successfully completed
SQL> select column_name, num_buckets, histogram from dba_tab_col_statistics where wner='SCOTT' and table_name='T';
COLUMN_NAMENUM_BUCKETS HISTOGRAM
------------------------------ ----------- ---------------
SYS_STUXJ8K0YTS_5QD1O0PEA514IY10 HEIGHT BALANCED
7、结论
在诸多Oracle dbms_stats参数中,method_opt灵活性很高。使用好这个参数,可以帮助我们更好的进行精确化统计量定义,提高SQL执行效率。
更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-05/101882.htm
