Oracle Index Clustering Factor(集群因子)
一、本文说明:
今天在做测试的时候发现字段上有索引,但是执行计划就是不走索引,经过在网上查找才发现原来是索引的集群因子过高导致的。本文属于转载
二、官网说明
The index clustering factor measures row order in relation to an indexed value suches employee last name.The more order that exists in rowstorage for this value,the lower the clustering factor.
----row存储的越有序,clustering factor的值越低。
The clustering factor is useful as a rough measure of the number of I/Os required to read an entire table by means of an index:
(1)、If the clustering factor is high,then Oracle Database performs a relatively high number of I/Os during a large index range scan.The index entriespoint to random table blocks,so the database may have to read and reread the same blocks over and over again to retrieve the data pointed to by the index.
----当clustering factor很高时,说明index entry (rowid) 是随机指向一些block的,在一个大的index range scan时,这样为了读取这些rowid指向的block,就需要一次又一次重复的去读这些block。
(2)、If the clustering factor is low,then Oracle Database performs a relatively low number of I/Os during a large index range scan.The index keys in arange tend to point to the same data blcok,so the database does not have to read and reread the same blocks over and over.
----当clustering factor值低时,说明index keys (rowid) 是指向的记录是存储在相同的block里,这样去读row时,只需要在同一个block里读取就可以了,这样减少重复读取blocks的次数。
The clustering factor is relevant for index scans because it can show:
(1)、Whether the database will use an index for large range scans;
(2)、The degree of table organization in relation to the index key;
(3)、Whether you should consider using an index-organized table,partitioning,or table cluster if rows must be ordered by the index key.
三、Index Clustering Factor说明
简单的说,Index Clustering Factor是通过一个索引扫描一张表,需要访问的表的数据块的数量,即对I/O的影响,也代表索引键存储位置是否有序。
(1)、如果越有序,即相邻的键值存储在相同的block,那么这时候Clustering Factor的值就越低;
(2)、如果不是很有序,即键值是随机的存储在block上,这样在读取键值时,可能就需要一次又一次的去访问相同的block,从而增加了I/O。
Clustering Factor的计算方式如下:
(1)、扫描一个索引(large index range scan);
(2)、比较某行的rowid和前一行的rowid,如果这两个rowid不属于同一个数据块,那么cluster factor增加1;
(3)、整个索引扫描完毕后,就得到了该索引的clustering factor。
如果clustering factor接近于表存储的块数,说明这张表是按照索引字段的顺序存储的。
如果clustering factor接近于行的数量,那说明这张表不是按索引字段顺序存储的。
在计算索引访问成本的时候,这个值十分有用。Clustering Factor乘以选择性参数(selectivity)就是访问索引的开销。
如果这个统计数据不能真实反映出索引的真实情况,那么可能会造成优化器错误的选择执行计划。另外如果某张表上的大多数访问是按照某个索引做索引扫描,那么将该表的数据按照索引字段的顺序重新组织,可以提高该表的访问性能。
四、测试
4.1、产生问题:

- ----查看一下数据库的版本----
1 SQL> select * from v$version where rownum=1;- 2
- 3 BANNER
- 4 --------------------------------------------------------------------------------
- 5 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
- 6
- ----创建一张测试表jack----
7 SQL> create table jack as select * from dba_objects where 1=2;- 8
- 9 Table created.
- 10
- ----将数据无序的插入jack表中----
11 SQL> begin- 12 2 for i in 1..10 loop
- 13 3 insert /*+ append */ into jack select * from dba_objects order by i;
- 14 4 commit;
- 15 5 end loop;
- 16 6 end;
- 17 7 /
- 18
- 19 PL/SQL procedure successfully completed.
- 20
- 21 SQL> select count(*) from jack;
- 22
- 23 COUNT(*)
- 24 ----------
- 25 725460
- 26
- ----查看一下表的大小-----
27 SQL> set wrap off- 28 SQL> col owner for a10;
- 29 SQL> col segment_name for a15;
- 30 SQL> select segment_name,blocks,extents,bytes/1024/1024||'M' "size" from user_segments where segment_name='JACK';
- 31
- 32 SEGMENT_NAME BLOCKS EXTENTS size
- 33 ------------- ---------- ---------- --------
34 JACK 11264 82 88M- 35
- ----在object_id上创建索引----
36 SQL> create index jack_ind on jack(object_id);- 37
- 38 Index created.
- 39
- ----查看一下索引的大小----
40 SQL> select segment_name,segment_type,blocks,extents,bytes/1024/1024||'M' "size" from user_segments where segment_name='JACK_IND';- 41
- 42 SEGMENT_NAME SEGMENT_TYPE BLOCKS EXTENTS size
- 43 ------------ ------------------ ---------- ---------- ---------
44 JACK_IND INDEX 1664 28 13M- ----在没有收集相关的统计信息之前,查看一下index clustering factor----
45 SQL> select index_name,clustering_factor,num_rows from user_indexes where index_name='JACK_IND';- 46
- 47 INDEX_NAME CLUSTERING_FACTOR NUM_ROWS
- 48 --------------- ----------------- ----------
- 49 JACK_IND 725460 725460
- 50
- ----简单的收集一下统计信息----
51 SQL> exec dbms_stats.gather_table_stats(user,'jack',cascade=>true);- 52
- 53 PL/SQL procedure successfully completed.
- 54
- ----再次查看index clustering factor----
55 SQL> select index_name,clustering_factor,num_rows from user_indexes where index_name='JACK_IND';- 56
- 57 INDEX_NAME CLUSTERING_FACTOR NUM_ROWS
- 58 -------------- ----------------- ----------
- 59 JACK_IND 725460 725460 ----显然统计信息收集前和后,clustering factor值不变,说在创建索引的时候,会收集表中的数据真正的行数。并且这里的clustering factor等num_rows,也说明表的clustering factor是无序的。
- 60
- ----查看一个确定值,然后查看执行计划----
61 SQL> explain plan for select * from jack where object_id=1501;- 62
- 63 Explained.
- 64
- 65 SQL> select * from table(dbms_xplan.display);
- 66
- 67 PLAN_TABLE_OUTPUT
- 68 --------------------------------------------------------------------------------
- 69 Plan hash value: 2860868395
- 70
- 71 --------------------------------------------------------------------------------
- 72 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Ti
- 73 --------------------------------------------------------------------------------
- 74 | 0 | SELECT STATEMENT | | 10 | 970 | 13 (0)| 00
- 75 | 1 | TABLE ACCESS BY INDEX ROWID| JACK | 10 | 970 | 13 (0)| 00
- 76 |* 2 | INDEX RANGE SCAN | JACK_IND | 10 | | 3 (0)| 00
- 77 --------------------------------------------------------------------------------
- 78
- 79 Predicate Information (identified by operation id):
- 80
- 81 PLAN_TABLE_OUTPUT
- 82 --------------------------------------------------------------------------------
- 83
84- 85 2 - access("OBJECT_ID"=1501)
- 86
- 87 14 rows selected. ----在这里走了索引,cost为13.
- 88
89 SQL> alter system flush buffer_cache;- 90
- 91 System altered.
- 92
- 93 SQL> set autotrace traceonly;
- ----查询一个范围的执行计划----
94 SQL> select * from jack where object_id>1000 and object_id<2000;- 95
- 96 9880 rows selected.
- 97
- 98
- 99 Execution Plan
- 100 ----------------------------------------------------------
- 101 Plan hash value: 949574992
- 102
- 103 --------------------------------------------------------------------------
- 104 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- 105 --------------------------------------------------------------------------
- 106 | 0 | SELECT STATEMENT | | 9657 | 914K| 1824 (1)| 00:00:22 |
- 107 |* 1 | TABLE ACCESS FULL| JACK | 9657 | 914K| 1824 (1)| 00:00:22 |
- 108 --------------------------------------------------------------------------
- 109
- 110 Predicate Information (identified by operation id):
- 111 ---------------------------------------------------
- 112
- 113 1 - filter("OBJECT_ID"<2000 AND "OBJECT_ID">1000)
- 114
- 115
- 116 Statistics
- 117 ----------------------------------------------------------
- 118 0 recursive calls
- 119 0 db block gets
- 120 10993 consistent gets
- 121 10340 physical reads
- 122 0 redo size
- 123 471945 bytes sent via SQL*Net to client
- 124 7657 bytes received via SQL*Net from client
- 125 660 SQL*Net roundtrips to/from client
- 126 0 sorts (memory)
- 127 0 sorts (disk)
- 128 9880 rows processed ----注意,object_id上是有索引的,但是这里并没有使用索引,而是使用了全表扫描。
- 129
- 130 SQL> alter system flush buffer_cache;
- 131
- 132 System altered.
- 133
- ----强制走索引,查看执行计划----
134 SQL> select /*+ index(jack jack_ind) */ * from jack where object_id>1000 and object_id<2000;- 135
- 136 9880 rows selected.
- 137
- 138
- 139 Execution Plan
- 140 ----------------------------------------------------------
- 141 Plan hash value: 2860868395
- 142
- 143 ----------------------------------------------------------------------------------------
- 144 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- 145 ----------------------------------------------------------------------------------------
- 146 | 0 | SELECT STATEMENT | | 9657 | 914K| 9683 (1)| 00:01:57 |
- 147 | 1 | TABLE ACCESS BY INDEX ROWID| JACK | 9657 | 914K| 9683 (1)| 00:01:57 |
- 148 |* 2 | INDEX RANGE SCAN | JACK_IND | 9657 | | 24 (0)| 00:00:01 |
- 149 ----------------------------------------------------------------------------------------
- 150
- 151 Predicate Information (identified by operation id):
- 152 ---------------------------------------------------
- 153
- 154 2 - access("OBJECT_ID">1000 AND "OBJECT_ID"<2000)
- 155
- 156
- 157 Statistics
- 158 ----------------------------------------------------------
- 159 0 recursive calls
- 160 0 db block gets
- 161 10561 consistent gets
- 162 164 physical reads
- 163 0 redo size
- 164 988947 bytes sent via SQL*Net to client
- 165 7657 bytes received via SQL*Net from client
- 166 660 SQL*Net roundtrips to/from client
- 167 0 sorts (memory)
- 168 0 sorts (disk)
- 169 9880 rows processed
----强制走索引之后,使用了index range scan,但是cost变成了9683,而全表扫描时是1824.
----还有比较一下两次查询中物理读的情况:全表扫描的物理读明显比索引的要高很多,但是Oracle却没有使用索引。
----因此Oracle认为走索引的Cost比走全表扫描大,而是大N倍,CBO是基于Cost来决定执行计划的。
----由此得出,对于索引的Cost,Oracle是根据clustering factor参数来计算的,而该实验中的clustering factor参数是很高的,数据存储无序。这就造成了Oracle认为走索引的cost比全表扫描的大。

4.2、解决问题:

- ----通过上面的分析,可以看出,要降低clustering factor才能解决问题,而要解决clustering factor,就需要重新对表的存储位置进行排序。----
----重建jakc表----
1 SQL> create table echo as select * from jack where 1=0;- 2
- 3 Table created.
- 4
- 5 SQL> insert /*+ append */ into echo select * from jack order by object_id;
- 6
- 7 725460 rows created.
- 8
- 9 SQL> commit;
- 10
- 11 Commit complete.
- 12
- 13 SQL> truncate table jack;
- 14
- 15 Table truncated.
- 16
- 17 SQL> insert /*+ append */ into jack select * from echo;
- 18
- 19 725460 rows created.
- 20
- 21 SQL> commit;
- 22
- 23 Commit complete.
- 24
- ----查看表和索引的信息----
25 SQL> select segment_name,blocks,extents,bytes/1024/1024||'M' "size" from user_segments where segment_name='JACK';- 26
- 27 SEGMENT_NAME BLOCKS EXTENTS size
- 28 ------------- ---------- ---------- -----------
29 JACK 11264 82 88M- 30
- 31 SQL> select segment_name,segment_type,blocks,extents,bytes/1024/1024||'M' "size" from user_segments where segment_name='JACK_IND';
- 32
- 33 SEGMENT_NAME SEGMENT_TYPE BLOCKS EXTENTS size
- 34 ------------ ------------------ ---------- ---------- -------------
35 JACK_IND INDEX 1536 27 12M- 36
- 37 SQL> select index_name,clustering_factor,num_rows from user_indexes where index_name='JACK_IND';
- 38
- 39 INDEX_NAME CLUSTERING_FACTOR NUM_ROWS
- 40 ------------- ----------------- ----------
- 41 JACK_IND 725460 725460
- 42
- ----对索引进行rebuild----
43 SQL> alter index jack_ind rebuild;- 44
- 45 Index altered.
- 46
- ----查看cluster factor----
47 SQL> select index_name,clustering_factor,num_rows from user_indexes where index_name='JACK_IND';- 48
- 49 INDEX_NAME CLUSTERING_FACTOR NUM_ROWS
- 50 --------------- ----------------- ----------
- 51 JACK_IND 10327 725460 ------注意这里的Factor,已经变成10327,我们收集一下表的统计信息,然后与表的block进行一次比较。
- 52
- 53 SQL> exec dbms_stats.gather_table_stats(user,'jack',cascade=>true);
- 54
- 55 PL/SQL procedure successfully completed.
- 56
- 57 SQL> select blocks from dba_tables where table_name='JACK';
- 58
- 59 BLOCKS
- 60 ----------
- 61 10474 ----表jack实际使用的block是10474,clustering factor是10327基本还是比较接近了,这也说明相邻的row是存储在相同的block里。
- 62
- 63 SQL> select index_name,clustering_factor,num_rows from user_indexes where index_name='JACK_IND';
- 64
- 65 INDEX_NAME CLUSTERING_FACTOR NUM_ROWS
- 66 ------------------------------ ----------------- ----------
- 67 JACK_IND 10327 725460
- 68
- 69 SQL> alter system flush buffer_cache;
- 70
- 71 System altered.
- 72
- 73 SQL> set autotrace traceonly;
- ----再次查看之前sql的执行计划----
74 SQL> select * from jack where object_id>1000 and object_id<2000;- 75
- 76 9880 rows selected.
- 77
- 78
- 79 Execution Plan
- 80 ----------------------------------------------------------
- 81 Plan hash value: 2860868395
- 82
- 83 ----------------------------------------------------------------------------------------
- 84 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- 85 ----------------------------------------------------------------------------------------
- 86 | 0 | SELECT STATEMENT | | 9657 | 914K| 162 (0)| 00:00:02 |
- 87 | 1 | TABLE ACCESS BY INDEX ROWID| JACK | 9657 | 914K| 162 (0)| 00:00:02 |
- 88 |* 2 | INDEX RANGE SCAN | JACK_IND | 9657 | | 24 (0)| 00:00:01 |
- 89 ----------------------------------------------------------------------------------------
- 90
- 91 Predicate Information (identified by operation id):
- 92 ---------------------------------------------------
- 93
- 94 2 - access("OBJECT_ID">1000 AND "OBJECT_ID"<2000)
- 95
- 96
- 97 Statistics
- 98 ----------------------------------------------------------
- 99 1 recursive calls
- 100 0 db block gets
- 101 1457 consistent gets
- 102 151 physical reads
- 103 0 redo size
- 104 988947 bytes sent via SQL*Net to client
- 105 7657 bytes received via SQL*Net from client
- 106 660 SQL*Net roundtrips to/from client
- 107 0 sorts (memory)
- 108 0 sorts (disk)
- 109 9880 rows processed
----注意这里的cost已经降到了162,性能提升还是非常明显。

五、小结
通过以上说明和测试,可以看到clustering factor也是索引健康的一个重要判断的标准。其值越低越好。它会影响CBO选择正确的执行计划。但是注意一点,clustering factor总是趋势与不断恶化的。
Oracle Index Clustering Factor(集群因子)的更多相关文章
- Oracle学习----集群因子(Clustering Factor)
1.集群因子的算法: 通过dbms_rowid.rowid_block_number(rowid)找到记录对应的block 号.索引中记录了rowid,因此oracle 就可以根据索引中的rowid来 ...
- 集群因子(Clustering Factor)
clustering factor是CBO使用的统计信息,用来衡量一个表中的列是否是规则排序存放的. 在通过索引访问表的时候,被用来作为代价评估的指示器.扫描索引的时候,clustering fact ...
- Clustering Factor——索引的成本指标
使用索引是我们面对海量数据搜索是一种常用的手段.通过有效的索引访问,可以使我们更快的访问到需要的数据,减少物理.逻辑IO,从而提高系统性能.在CBO时代,Oracle对于提交SQL的执行路径是有所选择 ...
- 使用Dapper读取Oracle多个结果集
Dapper对SQL Server支持很好,但对于Oracle有些用法不一样,需要自己进行特殊处理. 1.首先要自定义一个Oracle参数类 public class OracleDynamicPar ...
- Oracle Database常用补丁集Patch号及各版本PSU
Oracle Database常用补丁集Patch号及各版本PSU------------------------------------------------------------------- ...
- Oracle index hint syntax
Question: I added an index hint in my query, but the hint is being ignored. What is the correct sy ...
- oracle返回多结果集
kavy 原文 oracle返回多结果集 Oracle存储过程: create or replace procedure P_Sel_TopCount2(in_top in number, out_c ...
- oracle 存储过程返回结果集 (转载)
好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...
- 安装ORACLE高可用RAC集群11g执行root脚本的输出信息
安装ORACLE高可用RAC集群11g执行root脚本的输出信息 作者:Eric 微信:loveoracle11g [root@node1 ~]# /u01/app/oraInventory/orai ...
随机推荐
- opencv3计算机视觉+Python(四)
使用分水岭和GrabCut算法进行物体分割 用GrabCut算法进行图像分割 在OpenCV中,实现了grabcut分割算法,该算法可以方便的分割出前景图像,操作简单,而且分割的效果很好.算法的原理参 ...
- node.js及node-inspector的调试方法
1.先运行 $ node --debug-brk test.js 2.再在新的窗口运行: $ node-inspector 3.再打开Chrome浏览器输入node-inspector提示的地址,就会 ...
- Activiti 5.16 流程图高亮追踪 中文乱码问题解决方法
最近研究activiti的高亮流程图,发现中文是乱码,为了让大家少走弯路共享出来. 本文包含三个主要技术点: 1.spring MVC架构下输出动态图片 2.获得activiti流程图的stream流 ...
- ajax跨域资源共享
一.同域发送数据 略 二.跨域发送数据 1.存在的问题 1.什么是同源策略 同源策略阻止从一个域名上加载的脚本获取或操作另一个域名上的文档属性.也就是说,受到请求的 URL 的域名必须与当前 Web ...
- Admin添加字段
后台扩展用户信息,注意要到settings里面进行设定,有关联和继承两种方式 首先的关联表可以关联到user表但,主键在user表当中,所以没法直接在user表当中看到相关信息,要是通过继承扩展的话, ...
- requirejs神奇问题,data-main修改后,刷新没有重新载入
同事在使用require的时候,在配置地方增加 urlArgs: "bust=" + (new Date()).getTime(), 然后问题又来了,这个相当于js版本的东东会把 ...
- Centos6.6安装mysql记录
一.环境介绍: 系统:Cerntos6.6 Mysql版本:mysql-5.6.34 二.安装操作: 1.卸载旧版本: rpm -qa |grep mysql mysql-server-5.1.73- ...
- PAT 天梯赛 L2-015. 互评成绩 【排序】
题目链接 https://www.patest.cn/contests/gplt/L2-015 思路 在求和的过程中 标记一下 最大值和最小值,在最后求平均的时候 用总和减去最大值和最小值 去除 (总 ...
- Linux服务器操作指南
1. linux下在某行的前一行或后一行添加内容 http://www.361way.com/sed-process-lines/2263.html
- sublime text C++
几乎每一门编程语言都是从"Hello, world!"学起的, 刚学编程的时候感觉有点枯燥, 对它不够重视. 可是到后来慢慢发现, 几乎我学到的每一个知识点, 在最开始都是经过 h ...