Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2582005
  • 博文数量: 323
  • 博客积分: 10211
  • 博客等级: 上将
  • 技术积分: 4934
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-27 14:56
文章分类

全部博文(323)

文章存档

2012年(5)

2011年(3)

2010年(6)

2009年(140)

2008年(169)

分类: Oracle

2009-05-24 00:10:06

  CBO下计算index成本的时候会用到索引的统计信息。索引的统计信息可以从user_indexes获得。比如:
 
SQL>select table_name,num_rows,avg_leaf_blocks_per_key,avg_data_blocks_per_key,
    clustering_factor from user_indexes;
 
这个clustering_factor代表什么意思呢?查了一下Reference,解释如下:
 
CLUSTERING_FACTOR
 
* NUMBER Indicates the amount of order of the rows in the table
based on the values of the index.
n If the value is near the number of blocks, then
the table is very well ordered. In this case, the
index entries in a single leaf block tend to point
to rows in the same data blocks.
n If the value is near the number of rows, then the
table is very randomly ordered. In this case, it is
unlikely that index entries in the same leaf block
point to rows in the same data blocks.
 
在网上找到biti的一篇文章解释如下:
clustering_factor 是表征表中数据的存储顺序和某索引字段顺序的符合程度。
oracle 按照索引块中存储的rowid 来识别相临的索引中记录 在 表block中是否为相同块,如果索引中
存在记录 rowid a,b,c,d……,若b 和 a 是同一个block,则比较 c 和 b,若这时不是同一个block,
则clustering_factor + 1 ,然后比较 d 和 c,若还不是同一个 block,则clustering_factor + 1 ……

这样计算下来,clustering_factor 应该是介于 表 block数量 和 表记录数之间 的一个值,若
clustering_factor 接近 表block数量,则说明表中数据具有比较好的跟索引字段一样排序顺序的存储,
通过索引进行 range scan 的代价比较小(需要读取的表块可能比较少),若clustering_factor 接近
row数量,则说明表中数据和索引字段排序顺序差异很大,杂乱无张。则通过索引进行 range scan 的代价
比较大(需要读取的表块可能更多)。
当然,在oracle 920 开始,对于cluster_factor 比较接近表块数量的根据索引的大范围查询做了特别的
处理,不再是读一个索引记录去搜索一个表记录了,而是成批处理(通过索引块一批rowid一次去表块中获
得一批记录),这样就大大节约了读的成本( consistent gets)。
 
 
另外这里还有两个字段需要注意一下。avg_leaf_blocks_per_key,avg_data_blocks_per_key
Reference的解释如下:
AVG_LEAF_BLOCKS_PER_KEY

NUMBER Average number of leaf blocks in which each distinct
value in the index appears, rounded to the nearest
integer. For indexes that enforce UNIQUE and
PRIMARY KEY constraints, this value is always 1.
 
AVG_DATA_BLOCKS_PER_KEY

NUMBER Average number of data blocks in the table that are
pointed to by a distinct value in the index rounded to
the nearest integer. This statistic is the average
number of data blocks that contain rows that contain
a given value for the indexed columns.
 
简单解释如下:
AVG_LEAF_BLOCKS_PER_KEY:每个索引值的平均叶块数目。
AVG_DATA_BLOCKS_PER_KEY:每个索引值的平均数据块数据。
阅读(1751) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~