淺談索引系列之聚簇因子(clustering_factor)
初次聽說聚簇因子,我相信大部分人都是丈二和尚摸不著頭腦,不知所云。然而當我們瞭解其真正含義後,也許會覺得這個高大上的名詞也沒有那麼神秘。廢話少說,上官方文件:
Index Clustering Factor
The measures row order in relation to an indexed value such as employee last name. The more order that exists in row storage for this value, the lower the 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
- 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.
- 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.
下面透過實驗作一個直觀的展現:
點選(此處)摺疊或開啟
-
drop table colocated;
-
create table colocated ( x int, y varchar2(2000));
-
begin
-
for i in 1..100000 loop
-
insert into colocated values(i,dbms_random.string('a',5));
-
end loop;
-
commit;
-
end;
-
/
-
alter table colocated add constraint colocated_pk primary key(x);
-
-
drop table disorganized;
-
create table disorganized as select x,y from colocated order by y;
-
alter table disorganized add constraint disorganized_pk primary key(x);
-
exec dbms_stats.gather_table_stats(USER,'COLOCATED',CASCADE=>TRUE);
-
exec dbms_stats.gather_table_stats(USER,'DISORGANIZED',CASCADE=>TRUE);
-
SELECT a.index_name,b.num_rows,b.blocks,a.clustering_factor FROM user_indexes a,user_tables b WHERE a.table_name=b.table_name AND a.table_name='COLOCATED';
-
INDEX_NAME NUM_ROWS BLOCKS CLUSTERING_FACTOR
-
------------------------------ ---------- ---------- -----------------
-
COLOCATED_PK 100000 244 219
-
-
SELECT a.index_name,b.num_rows,b.blocks,a.clustering_factor FROM user_indexes a,user_tables b WHERE a.table_name=b.table_name AND a.table_name='DISORGANIZED';
-
-
INDEX_NAME NUM_ROWS BLOCKS CLUSTERING_FACTOR
-
------------------------------ ---------- ---------- -----------------
- DISORGANIZED_PK 100000 232 99572
瞭解了聚簇因子的概念,具體聚簇因子有什麼用途呢?下面進一步說明。
點選(此處)摺疊或開啟
-
set autotrace traceonly;
- SQL> select * from COLOCATED where x between 20000 and 20050;
- 51 rows selected.
-
-
Execution Plan
-
----------------------------------------------------------
-
Plan hash value: 1550765370
-
-
--------------------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-
--------------------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 52 | 572 | 3 (0)| 00:00:01 |
-
| 1 | TABLE ACCESS BY INDEX ROWID| COLOCATED | 52 | 572 | 3 (0)| 00:00:01 |
-
|* 2 | INDEX RANGE SCAN | COLOCATED_PK | 52 | | 2 (0)| 00:00:01 |
-
--------------------------------------------------------------------------------------------
-
-
Predicate Information (identified by operation id):
-
---------------------------------------------------
-
-
2 - access("X">=20000 AND "X"<=20050)
-
-
-
Statistics
-
----------------------------------------------------------
-
0 recursive calls
-
0 db block gets
-
11 consistent gets
-
0 physical reads
-
0 redo size
-
2000 bytes sent via SQL*Net to client
-
556 bytes received via SQL*Net from client
-
5 SQL*Net roundtrips to/from client
-
0 sorts (memory)
-
0 sorts (disk)
-
51 rows processed
-
- SQL> select * from DISORGANIZED where x between 20000 and 20050;
- 51 rows selected.
-
Execution Plan
-
----------------------------------------------------------
-
Plan hash value: 2594580634
-
-
-----------------------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-
-----------------------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 52 | 572 | 54 (0)| 00:00:01 |
-
| 1 | TABLE ACCESS BY INDEX ROWID| DISORGANIZED | 52 | 572 | 54 (0)| 00:00:01 |
-
|* 2 | INDEX RANGE SCAN | DISORGANIZED_PK | 52 | | 2 (0)| 00:00:01 |
-
-----------------------------------------------------------------------------------------------
-
-
Predicate Information (identified by operation id):
-
---------------------------------------------------
-
-
2 - access("X">=20000 AND "X"<=20050)
-
-
-
Statistics
-
----------------------------------------------------------
-
1 recursive calls
-
0 db block gets
-
56 consistent gets
-
0 physical reads
-
0 redo size
-
2000 bytes sent via SQL*Net to client
-
556 bytes received via SQL*Net from client
-
5 SQL*Net roundtrips to/from client
-
0 sorts (memory)
-
0 sorts (disk)
- 51 rows processed
COLOCATED和DISORGANIZED兩張表儲存了同樣的語句,相同的SQL語句執行,聚簇因子小的表邏輯讀為11,聚簇因子大的邏輯讀卻為56,大家都知道邏輯讀和鎖機制息息相關,鎖會影響到了資料庫的併發性,也會影響效能。
小結:
1.聚簇因子的高低會影響執行計劃的選擇。
2.聚簇因子與表的儲存有關,無法透過重建索引來改變聚簇因子的大小。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29827284/viewspace-2134262/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 淺談聚簇索引與非聚簇索引索引
- 關於索引聚簇因子(CLUSTERING_FACTOR)引起的sql優化過程索引SQL優化
- 聚簇因子的理解
- ORACLE 聚簇因子優化Oracle優化
- clustering factor索引聚簇因子和執行計劃索引
- 聚簇索引索引
- MySQL中的聚簇索引和非聚簇索引MySql索引
- 淺談索引系列之索引重建索引
- MySQL聚簇索引和非聚簇索引的原理及使用MySql索引
- 資料庫表,索引(索引組織表,聚簇表,聚簇索引,)資料庫索引
- InnoDB學習(八)之 聚簇索引索引
- 一分鐘明白MySQL聚簇索引和非聚簇索引MySql索引
- 聚簇索引和非聚簇索引到底有什麼區別?索引
- MySQL 聚簇索引 和覆蓋索引MySql索引
- 聚簇因子和執行計劃的聯絡
- 資料庫聚簇索引——not null條件對唯一鍵索引成為聚簇索引的影響資料庫索引Null
- 淺談索引系列之基本原理索引
- 一看就懂的MySQL的聚簇索引,以及聚簇索引是如何長高的MySql索引
- 【Mysql】InnoDB 中的聚簇索引、二級索引、聯合索引MySql索引
- MySQL 聚簇索引一定是主鍵嗎MySql索引
- MySQL innodb如何選擇一個聚簇索引MySql索引
- 淺談索引系列之本地索引與全域性索引索引
- 【實驗】【聚簇】聚簇(Cluster)和聚簇表(Cluster Table)的建立與總結
- Oracle聚簇表Oracle
- 【效能優化】Oracle 效能優化:降低列值聚簇因子 提高查詢效率優化Oracle
- 理解索引和聚簇——效能調整手冊和參考索引
- 聚簇表簡介
- sql server 索引闡述系列七 索引填充因子與碎片SQLServer索引
- 【效能最佳化】Oracle 效能最佳化:降低列值聚簇因子 提高查詢效率Oracle
- 淺談Mysql索引MySql索引
- 淺談sql索引SQL索引
- [MySQL]為什麼非聚簇索引不儲存資料位置MySql索引
- 聊聊Oracle聚簇Cluster(上)Oracle
- oracle 聚簇表學習Oracle
- MySQL淺談(索引、鎖)MySql索引
- 深度聚類演算法淺談聚類演算法
- 淺談索引序列之是否可以儲存NULL值?索引Null
- 一次簡單的效能優化診斷,聚簇因子過高導致全表掃描。優化