收集对象统计信息【表、列、索引】

来源:互联网 发布:php众筹源码 编辑:程序博客网 时间:2024/05/21 21:46

一、建表

CREATE TABLE TABLE1( DATE_ORD DATE NOT NULL, SEQ_ORD NUMBER(6) NOT NULL, SMALL_VC VARCHAR2(5))PCTFREE 90PCTUSED 10;

二、创建索引

create index nonuni_index tablespace users;

三、收集并查看表的统计信息

分析表:

--方法一:analyze table TABLE1 compute statistics;//完全统计--方法二:analyze table TABLE1 estimate statistics sample 20 percent;//抽样统计--方法三:dbms_stats.gather_table_stats('SH','TABLE1');

查看表的统计信息:

select table_name,--表名       num_rows,--记录数       blocks,--数据块数       avg_row_len--平均行长from dba_tableswhere table_name = 'TABLE1';

四、收集并查看列的统计信息

分析表:

dbms_stats.gather_table_stats('SH',--用户名'TABLE1',--表名cascade =>true,--是否同时收集索引信息estimate_percent =>null,--抽样统计,null表示完全统计method_opt =>'for columns SEQ_ORD size 10');

查看列的统计信息:

select table_name ,--表名       column_name,--列名       num_distinct,--不同的列值个数       density,--列的选择率       num_nulls,--空列的个数       avg_col_len,--平均列长度       histogram--直方图from dba_tab_columnswhere table_name = 'TABLE1';

查看列的直方图:

select rownum ,--频率直方图号       begin_num,--开始       end_num,--结束       end_num-begin_num  width,--宽度       round(100000/(end_num-begin_num),2)--高度from(   select endpoint_value end_num,          lag(endpoint_value,1)over(order by endpoint_number) begin_num  from dba_tab_histograms  where table_name ='TABLE1'  and column_name ='SEQ_ORD')where end_num is not nullorder by begin_num nulls first;

五、查看索引的统计信息

分析索引:

--方法一:analyze index nonuni_index compute statistics;//完全统计--方法二:analyze index nonuni_index estimate statistics sample 20 percent;//抽样统计--方法三:dbms_stats.gather_index_stats('SH','NONUNI_INDEX');

查看索引的统计信息:

select index_name,--索引名       blevel,--索引的层数       leaf_blocks,--叶节点数       distinct_keys,--不同键值的数量       avg_leaf_blocks_per_key,--每个键值的平均叶块数       avg_data_blocks_per_key,--每个键值的平均数据块数       clustering_factor,--聚簇因子       num_rows--索引的记录数from dba_indexeswhere index_name = 'NONUNI_INDEX';