Oracle Index Clustering Factor(集群因子)

╰半橙微兮° 2021-12-17 17:02 336阅读 0赞

一、本文说明:

  1. 今天在做测试的时候发现字段上有索引,但是执行计划就是不走索引,经过在网上查找才发现原来是索引的集群因子过高导致的。本文属于转载+模拟。

二、官网说明

  1. 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.
  2. ----row存储的越有序,clustering factor的值越低。
  3. 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:
  4. (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.
  5. ----当clustering factor很高时,说明index entry (rowid) 是随机指向一些block的,在一个大的index range scan时,这样为了读取这些rowid指向的block,就需要一次又一次重复的去读这些block
  6. (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.
  7. ----当clustering factor值低时,说明index keys (rowid) 是指向的记录是存储在相同的block里,这样去读row时,只需要在同一个block里读取就可以了,这样减少重复读取blocks的次数。
  8. The clustering factor is relevant for index scans because it can show:
  9. (1)、Whether the database will use an index for large range scans;
  10. (2)、The degree of table organization in relation to the index key;
  11. (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说明

  1. 简单的说,Index Clustering Factor是通过一个索引扫描一张表,需要访问的表的数据块的数量,即对I/O的影响,也代表索引键存储位置是否有序。
  2. (1)、如果越有序,即相邻的键值存储在相同的block,那么这时候Clustering Factor的值就越低;
  3. (2)、如果不是很有序,即键值是随机的存储在block上,这样在读取键值时,可能就需要一次又一次的去访问相同的block,从而增加了I/O
  4. Clustering Factor的计算方式如下:
  5. (1)、扫描一个索引(large index range scan);
  6. (2)、比较某行的rowid和前一行的rowid,如果这两个rowid不属于同一个数据块,那么cluster factor增加1
  7. (3)、整个索引扫描完毕后,就得到了该索引的clustering factor
  8. 如果clustering factor接近于表存储的块数,说明这张表是按照索引字段的顺序存储的。
  9. 如果clustering factor接近于行的数量,那说明这张表不是按索引字段顺序存储的。
  10. 在计算索引访问成本的时候,这个值十分有用。Clustering Factor乘以选择性参数(selectivity)就是访问索引的开销。
  11. 如果这个统计数据不能真实反映出索引的真实情况,那么可能会造成优化器错误的选择执行计划。另外如果某张表上的大多数访问是按照某个索引做索引扫描,那么将该表的数据按照索引字段的顺序重新组织,可以提高该表的访问性能。

四、测试

4.1、产生问题:

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

4.2、解决问题:

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

——注意这里的cost已经降到了162,性能提升还是非常明显。

五、小结

  1. 通过以上说明和测试,可以看到clustering factor也是索引健康的一个重要判断的标准。其值越低越好。它会影响CBO选择正确的执行计划。但是注意一点,clustering factor总是趋势与不断恶化的。

转载于:https://www.cnblogs.com/Richardzhu/articles/2874972.html

发表评论

表情:
评论列表 (有 0 条评论,336人围观)

还没有评论,来说两句吧...

相关阅读