SQL> drop table test purge; Table dropped. SQL> create table test as select * from dba_objects; Table created. SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 no rows selected 此时没有直方图的信息 BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'TEST', tabname => 'TEST', estimate_percent => 100, method_opt => 'for all columns size skewonly', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; method_opt => 'for all columns size skewonly'---这种方式基本上对所有的列都搜集了直方图信息 SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- OWNER 72530 30 .04 FREQUENCY 30 OBJECT_NAME 72530 44279 61.05 HEIGHT BALANCED 254 SUBOBJECT_NAME 72530 114 .16 FREQUENCY 114 OBJECT_ID 72530 72530 100 NONE 1 DATA_OBJECT_ID 72530 7687 10.6 HEIGHT BALANCED 254 OBJECT_TYPE 72530 44 .06 FREQUENCY 44 CREATED 72530 1093 1.51 HEIGHT BALANCED 254 LAST_DDL_TIME 72530 1143 1.58 HEIGHT BALANCED 254 TIMESTAMP 72530 1214 1.67 HEIGHT BALANCED 254 STATUS 72530 2 0 FREQUENCY 2 TEMPORARY 72530 2 0 FREQUENCY 2 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- GENERATED 72530 2 0 FREQUENCY 2 SECONDARY 72530 2 0 FREQUENCY 2 NAMESPACE 72530 21 .03 FREQUENCY 21 EDITION_NAME 72530 0 0 NONE 0 15 rows selected. 可以看到基本上对所有列都搜集了统计信息 SQL> drop table test purge; Table dropped. SQL> create table test as select * from dba_objects; Table created. --------------------------------------------------------------------------------------------------------- SQL> BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'TEST', tabname => 'TEST', estimate_percent => 100, method_opt => 'for all columns size auto', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; 2 3 4 5 6 7 8 9 10 / PL/SQL procedure successfully completed. SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- OWNER 72530 30 .04 NONE 1 OBJECT_NAME 72530 44279 61.05 NONE 1 SUBOBJECT_NAME 72530 114 .16 NONE 1 OBJECT_ID 72530 72530 100 NONE 1 DATA_OBJECT_ID 72530 7687 10.6 NONE 1 OBJECT_TYPE 72530 44 .06 NONE 1 CREATED 72530 1093 1.51 NONE 1 LAST_DDL_TIME 72530 1143 1.58 NONE 1 TIMESTAMP 72530 1214 1.67 NONE 1 STATUS 72530 2 0 NONE 1 TEMPORARY 72530 2 0 NONE 1 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- GENERATED 72530 2 0 NONE 1 SECONDARY 72530 2 0 NONE 1 NAMESPACE 72530 21 .03 NONE 1 EDITION_NAME 72530 0 0 NONE 0 15 rows selected. SQL> select count(*) from test where owner='SYS'; COUNT(*) ---------- 30795 SQL> BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'TEST', tabname => 'TEST', estimate_percent => 100, method_opt => 'for all columns size auto', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; 2 3 4 5 6 7 8 9 10 11 / PL/SQL procedure successfully completed. SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- OWNER 72530 30 .04 FREQUENCY 30 OBJECT_NAME 72530 44279 61.05 NONE 1 SUBOBJECT_NAME 72530 114 .16 NONE 1 OBJECT_ID 72530 72530 100 NONE 1 DATA_OBJECT_ID 72530 7687 10.6 NONE 1 OBJECT_TYPE 72530 44 .06 NONE 1 CREATED 72530 1093 1.51 NONE 1 LAST_DDL_TIME 72530 1143 1.58 NONE 1 TIMESTAMP 72530 1214 1.67 NONE 1 STATUS 72530 2 0 NONE 1 TEMPORARY 72530 2 0 NONE 1 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- GENERATED 72530 2 0 NONE 1 SECONDARY 72530 2 0 NONE 1 NAMESPACE 72530 21 .03 NONE 1 EDITION_NAME 72530 0 0 NONE 0 auto的方式 有where条件去搜集 --------------------------------------------------------------------------------------------------- SQL> drop table test purge; Table dropped. SQL> create table test as select * from dba_objects; Table created. SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 no rows selected SQL> BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'TEST', tabname => 'TEST', estimate_percent => 100, method_opt => 'for columns owner size skewonly', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; 2 3 4 5 6 7 8 9 10 11 / PL/SQL procedure successfully completed. SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- OWNER 72530 30 .04 FREQUENCY 30 SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- OWNER 72530 30 .04 FREQUENCY 30 SQL> BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'TEST', tabname => 'TEST', estimate_percent => 100, method_opt => 'for all columns size repeat', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; 2 3 4 5 6 7 8 9 10 11 / PL/SQL procedure successfully completed. SQL> select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a.histogram, a.num_buckets from dba_tab_col_statistics a, dba_tables b where a.owner = b.owner and a.table_name = b.table_name and a.owner = 'TEST' and a.table_name = 'TEST'; 2 3 4 5 6 7 8 9 10 11 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- OWNER 72530 30 .04 FREQUENCY 30 OBJECT_NAME 72530 44279 61.05 NONE 1 SUBOBJECT_NAME 72530 114 .16 NONE 1 OBJECT_ID 72530 72530 100 NONE 1 DATA_OBJECT_ID 72530 7687 10.6 NONE 1 OBJECT_TYPE 72530 44 .06 NONE 1 CREATED 72530 1093 1.51 NONE 1 LAST_DDL_TIME 72530 1143 1.58 NONE 1 TIMESTAMP 72530 1214 1.67 NONE 1 STATUS 72530 2 0 NONE 1 TEMPORARY 72530 2 0 NONE 1 COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM NUM_BUCKETS ------------------------------ ---------- ----------- ----------- --------------- ----------- GENERATED 72530 2 0 NONE 1 SECONDARY 72530 2 0 NONE 1 NAMESPACE 72530 21 .03 NONE 1 EDITION_NAME 72530 0 0 NONE 0 method_opt => 'for all columns size repeat',重复上次搜集过的统计信息