index clustering factor

index clustering factor,指的是,表中数据的顺序和索引中数据的顺序的相似度。相似度越高,聚簇因子就越小。相似度越小,聚簇因子就越高。

参考文档:

https://docs.oracle.com/database/121/CNCPT/indexiot.htm#CNCPT89180

Index Clustering Factor

The index clustering factor measures row order in relation to an indexed value such as employee last name. As the degree of order increases, the clustering factor decreases.

The clustering factor is useful as a rough measure of the number of I/Os required to read an entire table using an index:

  • 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 entries point 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.

       -- 上面语句的备注(聚簇因子高,说明数据的顺序和索引的顺序很不相似,索引扫描会增加IO,需要通过索引不停地取数据块)

  • 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 a range tend to point to the same data block, so the database does not have to read and reread the same blocks over and over.

      -- 上面的语句的备注(聚簇因子低,说明数据的顺序和索引的顺序相似,索引扫描不会需要太多的IO,直接从索引中取就可以了,个人理解,可能不太准确)

The clustering factor is relevant for index scans because it can show:

  • Whether the database will use an index for large range scans

  • The degree of table organization in relation to the index key

  • Whether you should consider using an index-organized table, partitioning, or table cluster if rows must be ordered by the index key

Example 3-4 Clustering Factor

Assume that the employees table fits into two data blocks. Table 3-1 depicts the rows in the two data blocks (the ellipses indicate data that is not shown).

Table 3-1 Contents of Two Data Blocks in the Employees Table

Data Block 1 Data Block 2
100 Steven    King    SKING    ...
156 Janette   King    JKING    ...
115 Alexander Khoo    AKHOO    ...
.
.
.
116 Shelli  Baida     SBAIDA   ...
204 Hermann Baer      HBAER    ...
105 David   Austin    DAUSTIN  ...
130 Mozhe   Atkinson  MATKINSO ...
166 Sundar  Ande      SANDE    ...
174 Ellen   Abel      EABEL    ...
 


149 Eleni    Zlotkey EZLOTKEY ...
200 Jennifer Whalen  JWHALEN  ...
.
.
.
137 Renske   Ladwig  RLADWIG  ...
173 Sundita  Kumar   SKUMAR   ...
101 Neena    Kochar  NKOCHHAR ...

Rows are stored in the blocks in order of last name (shown in bold). For example, the bottom row in data block 1 describes Abel, the next row up describes Ande, and so on alphabetically until the top row in block 1 for Steven King. The bottom row in block 2 describes Kochar, the next row up describes Kumar, and so on alphabetically until the last row in the block for Zlotkey.

-- 上面的语句,block1和block2 中的,索引是lastname,lastname自下而上顺序排列。

Assume that an index exists on the last name column. Each name entry corresponds to a rowid. Conceptually, the index entries would look as follows:   -- 索引中的数据排列如下,按照lastname 排序。

Abel,block1row1
Ande,block1row2
Atkinson,block1row3
Austin,block1row4
Baer,block1row5
.
.
.

Assume that a separate index exists on the employee ID column. Conceptually, the index entries might look as follows, with employee IDs distributed in almost random locations throughout the two blocks:  -- employee id列上又存在索引,索引中的数据按照employee id排列如下:

100,block1row50
101,block2row1
102,block1row9
103,block2row19
104,block2row39
105,block1row4
.
.
.

The following statement queries the ALL_INDEXES view for the clustering factor for these two indexes:

-- 查询索引的聚簇因子

SQL> SELECT INDEX_NAME, CLUSTERING_FACTOR 
  2  FROM ALL_INDEXES 
  3  WHERE INDEX_NAME IN ('EMP_NAME_IX','EMP_EMP_ID_PK');
 
INDEX_NAME           CLUSTERING_FACTOR
-------------------- -----------------
EMP_EMP_ID_PK                       19
EMP_NAME_IX                          2

The clustering factor for EMP_NAME_IX is low, which means that adjacent index entries in a single leaf block tend to point to rows in the same data blocks. The clustering factor for EMP_EMP_ID_PK is high, which means that adjacent index entries in the same leaf block are much less likely to point to rows in the same data blocks.

可以看到 索引emp_name_ix的聚簇因子很低, 说明数据顺序和索引中的数据顺序很相似。从上面的图中可以看出 。

索引emp_emp_id的聚簇因子很高,说明数据顺序和索引中的数据顺序很不相似(顺序相差较大)。从上面图中可以看出,该索引中数据的排列顺序和数据排列顺序相差较大。

-- 先把官方文档弄出来,后续再补充测试案例。

END

-- 2019-11-13 add

-- 以下内容摘自MOS Clustering Factor (Doc ID 39836.1)

Interpretation

If the count is close to the number of blocks in the table then the index is well ordered. This is true because the counter only gets incremented when the actual data is found in a different block from the last piece of row data. What this means is that when an index block is read, the table lookups required based on that block are likely to be in the same table blocks. So, the lower the Clustering Factor the less I/O is likely to be used by the statement and so the optimizer is more likely to choose that index.

If the count is close to the number of rows in the table then the index is less well ordered. In this case adjacent index entries do not tend to point to the same block in the table, thus more block reads are likely to be required. An index with a higher clustering factor is more likely to have to revisit Data blocks than with a lower Clustering factor .

The clustering factor can be used by the optimizer to adjust the potential number of blocks that will be accessed by a particular predicate or query. This is useful for determining the number of base table blocks that will be visited when accessed via the index.

The clustering factor is effectively a count of the number of data blocks visited as the result of an index lookup. Multiplying the clustering factor by the selectivity will give the cost of the operation. It is predominately used to calculate costs for index range scans.

Common Questions

  • How can Clustering Factor be Reduced

    The only method to affect the clustering factor is to sort and then store the rows in the table in the same order as in they appear in the index. Exporting rows and putting them back in the same order that they appeared originally will have no affect. Remember that ordering the rows to suit one index may have detrimental effects on the choice of other indexes.

END

发布了754 篇原创文章 · 获赞 31 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/102745267