聚集因子 cluster factor
阿新 • • 發佈:2018-12-06
一:what
1: 聚集因子 是索引資料順序和表順序的一致性程度
2: 取值範圍為:最小資料塊數,最大為行數,值越小越好!
二:why
1:聚集因子會影響sql的執行效率
2:比如表結構,資料量,索引都一樣,生產和測試環境sql執行效率大不一致,
這種情況下就可能是聚集因子不一致導致的
三:how
drop table t_test;
create table t_test(t_id number(20),t_num number(20));
insert into t_test
select level,dbms_random.random from dual connect by level <= 1000000;
create index t_test_idx on t_test(t_id);
drop table t_test2;
create table t_test2(t_id number(20),t_num number(20));
insert into t_test2
select * from t_test order by dbms_random.random;
create index t_test2_idx on t_test2(t_id);
explain plan for select * from t_test where t_id between 2000 and 9000;
select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1773029164
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8315 | 211K| 48 (0)|
| 1 | TABLE ACCESS BY INDEX ROWID| T_TEST | 8315 | 211K| 48 (0)|
|* 2 | INDEX RANGE SCAN | T_TEST_IDX | 8315 | | 24 (0)|
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T_ID">=2000 AND "T_ID"<=9000)
Note
-----
- dynamic sampling used for this statement (level=2)
18 rows selected
--執行計劃
explain plan for select * from t_test2 where t_id between 2000 and 9000;
select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1680898452
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6904 | 175K| 668 (3)| 00:00:09 |
|* 1 | TABLE ACCESS FULL| T_TEST2 | 6904 | 175K| 668 (3)| 00:00:09 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("T_ID">=2000 AND "T_ID"<=9000)
Note
-----
- dynamic sampling used for this statement (level=2)
17 rows selected
--檢視聚集因子
SELECT index_name,table_name,blevel,leaf_blocks,clustering_factor,num_rows
FROM user_indexes
WHERE lower(table_name) LIKE 't_test%';
INDEX_NAME TABLE_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR NUM_ROWS
------------------------------ ------------------------------ ---------- ----------- ----------------- ----------
T_TEST_IDX T_TEST 2 2226 2401 1000000
T_TEST2_IDX T_TEST2 2 2226 999612 1000000
分析:
1:一個索引範圍掃描獲取資料,另一個全表掃描
2:cost 一個48,另外一個668,效能提升14倍!