索引质量的高低对数据库整体性能有着直接的影响。良好高质量的索引使得数据库性能得以数量级别的提升,而低效冗余的索引则使得数据库性能缓慢如牛,即便是使用高档的硬件配置。因此对于索引在设计之初需要经过反复的测试与考量。那对于已经置于生产环境中的数据库,我们也可以通过查询相关数据字典得到索引的质量的高低,通过这个分析来指导如何改善索引的性能。下面给出了演示以及索引创建的基本指导原则,最后给出了索引质量分析脚本。
1、查看索引质量
--获取指定schema或表上的索引质量信息报告
gx_adm@cabo3> @idx_quality
enter value for input_owner: gx_adm
enter value for input_tbname: client_trade_tbl -->如果我们省略具体的表名则会输出整个schema的索引质量报告
table table index data blks leaf blks clust index
table rows blocks index size mb per key per key factor quality
------------------------- ------------ ---------- ------------------------- ------- --------- --------- ------------ -------------
client_trade_tbl 6,318,035 278488 i_tdcl_arc_stl_date_stock 62 312 13 171,017 5-excellent
i_tdcl_arc_stl_date_cash 62 318 13 174,599 5-excellent
i_tdcl_arc_cancel_date 83 238 8 288,678 5-excellent
i_tdcl_arc_input_date 144 249 13 310,974 5-excellent
i_tdcl_arc_trade_date 144 269 14 337,097 5-excellent
pk_client_trade_tbl 200 1 1 798,216 2-good
i_tdcl_arc_grp_ref_id 144 1 1 811,468 2-good
uni_tdcl_arc_ref_id 136 1 1 765,603 2-good
i_tdcl_arc_contract_num 72 1 1 834,491 2-good
i_tdcl_arc_settled_date 61 299 5 380,699 1-poor
i_tdcl_arc_acc_num 184 624 3 3,899,446 1-poor
i_tdcl_arc_pl_stk 176 218 1 4,348,804 1-poor
i_tdcl_arc_instru_id 120 2,667 8 4,273,038 1-poor
--从上面的单表输出的索引质量可知,出现了4个处于poor级别的索引,也就是说这些个索引具有较大的聚簇因子,几乎接近于表上的行了
--对于这几个索引的质量还应结合该索引的使用频率来考量该索引存在的必要性
--对于聚簇因子,只能通过重新组织表上的数据来,以及调整相应索引列的顺序得以改善
--查询单表上索引列的相关信息
gx_adm@cabo3> @idx_info
enter value for owner: gx_adm
enter value for table_name: client_trade_tbl
table_name index_name cl_nam cl_pos status idx_typ dscd
------------------------- ------------------------------ -------------------- ------ -------- --------------- ----
client_trade_tbl i_tdcl_arc_acc_num acc_num 1 valid normal asc
i_tdcl_arc_cancel_date cancel_date 1 valid normal asc
i_tdcl_arc_contract_num contract_num 1 valid normal asc
i_tdcl_arc_grp_ref_id grp_ref_id 1 valid normal asc
i_tdcl_arc_input_date input_date 1 valid normal asc
i_tdcl_arc_instru_id instru_id 1 valid normal asc
i_tdcl_arc_pl_stk stock_cd 1 valid normal asc
i_tdcl_arc_pl_stk pl_cd 2 valid normal asc
i_tdcl_arc_settled_date settled_date 1 valid normal asc
i_tdcl_arc_stl_date_cash stl_date_cash 1 valid normal asc
i_tdcl_arc_stl_date_stock stl_date_stock 1 valid normal asc
i_tdcl_arc_trade_date trade_date 1 valid normal asc
pk_client_trade_tbl business_date 1 valid normal asc
pk_client_trade_tbl ref_id 2 valid normal asc
uni_tdcl_arc_ref_id ref_id 1 valid normal asc
--从上面的查询结果可知,当前表trade_client_tbl上含有13个索引,应该来说该表索引存在一定冗余。
--大多数情况下,单表上6-7个索引是比较理想的。过多的索引导致过大的资源开销,以及降低dml性能。
2、索引创建的基本指导原则
索引的创建应遵循精而少的原则
收集表上所有查询的各种不同组合,找出具有最佳离散度的列(或主键列等)创建单索引
对于频繁读取而缺乏比较理想离散值的列为其创建组合索引
对于组合索引应考虑下列因素来制定合理的索引列顺序,以下优先级别由高到低来作为索引的前导列,第二列等等
列被使用的频率
该列是否经常使用“ = ”作为常用查询条件
列上的离散度
组合列经常按何种顺序排序
哪些列会作为附件性列被添加
3、索引质量分析脚本
--script name: idx_quality.sql --author : leshami --blog: http://blog.csdn.net/leshami
--index quality retrieval
set linesize 145
set pagesize 1000
set verify off
clear computes
clear breaks
break on table_name on num_rows on blocks
column owner format a14 heading 'index owner'
column table_name format a25 heading 'table'
column index_name format a25 heading 'index'
column num_rows format 999g999g990 heading 'table|rows'
column mb format 9g990 heading 'index|size mb'
column blocks heading 'table|blocks'
column num_blocks format 9g990 heading 'data|blocks'
column avg_data_blocks_per_key format 999g990 heading 'data blks|per key'
column avg_leaf_blocks_per_key format 999g990 heading 'leaf blks|per key'
column clustering_factor format 999g999g990 heading 'clust|factor'
column index_quality format a13 heading 'index|quality'
--spool index_quality
select i.table_name,
t.num_rows,
t.blocks,
i.index_name,
o.bytes / 1048576 mb,
i.avg_data_blocks_per_key,
i.avg_leaf_blocks_per_key,
i.clustering_factor,
case
when nvl (i.clustering_factor, 0) = 0 then '0-no stats'
when nvl (t.num_rows, 0) = 0 then '0-no stats'
when (round (i.clustering_factor / t.num_rows * 100)) < 6 then '5-excellent'
when (round (i.clustering_factor / t.num_rows * 100)) between 7 and 11 then '4-very good'
when (round (i.clustering_factor / t.num_rows * 100)) between 12 and 15 then '2-good'
when (round (i.clustering_factor / t.num_rows * 100)) between 16 and 25 then '2-fair'
else '1-poor'
end
index_quality
from dba_indexes i, dba_segments o, dba_tables t
where
-- i.index_name like upper ('%&&1%') and
i.owner = t.owner
and i.table_name = t.table_name
and i.owner = o.owner
and i.index_name = o.segment_name
and t.owner = upper('&input_owner')
and t.table_name like upper('%&input_tbname%')
order by table_name,
num_rows,
blocks,
index_quality desc;
--spool off;
===========================================================================================
--script name: idx_info.sql
--get the index column information by specified table
set linesize 180
col cl_nam format a20
col table_name format a25
col cl_pos format 9
col idx_typ format a15
select b.table_name,
a.index_name,
a.column_name cl_nam,
a.column_position cl_pos,
b.status,
b.index_type idx_typ,
a.descend dscd
from dba_ind_columns a, dba_indexes b
where a.index_name = b.index_name
and owner = upper('&owner')
and a.table_name like upper('%&table_name%')
order by 2, 4;