Oracle索引組合字段的位置不同,當查詢條件不能覆蓋索引時,影響查詢效率。查詢條件是不是索引字段的第一列影響執行計划,實驗驗證
實驗1:查詢條件為組合索引的第一列
--創建測試表 create table test_C as select * from user_objects where 1=2; --創建測試表 create table test_a as select * from user_objects where 1=1; --創建組合索引 create index test_a_1 on test_a(object_id,object_type); set linesize 1000; set autotrace trace; select * from test_a where object_id=161728; **************************************************** 執行計划 ---------------------------------------------------------- Plan hash value: 3036018411 ------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 116 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST_A | 1 | 116 | 3 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | TEST_A_1 | 1 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=161728) 統計信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 3 consistent gets 0 physical reads 0 redo size 1933 bytes sent via SQL*Net to client 500 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
查詢條件object_id為索引的前導列。執行計划:INDEX RANGE SCAN,cost是3,邏輯讀是3.
drop index test_a_1; drop index test_a_2; --創建索引2,查詢條件在索引中的位置是第二列 create index test_a_2 on test_a(object_name,object_ID); --再次查詢 select * from test_a where object_id=161728; **************************************************** 執行計划 ---------------------------------------------------------- Plan hash value: 219338936 ------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 116 | 7 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST_A | 1 | 116 | 7 (0)| 00:00:01 | |* 2 | INDEX SKIP SCAN | TEST_A_2 | 1 | | 6 (0)| 00:00:01 | ------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=161728) filter("OBJECT_ID"=161728) 統計信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 8 consistent gets 0 physical reads 0 redo size 1933 bytes sent via SQL*Net to client 500 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
查詢條件object_id為索引的第二列。執行計划:INDEX SKIP SCAN,cost是7,邏輯讀是8.
同樣object_id作為查詢條件,當object_id作為索引的第一列或者作為索引的第二列,它們在cost與邏輯讀上是有區別的,也就是索引跳躍掃描(INDEX SKIP SCAN)沒有索引范圍掃描(INDEX RANGE SCAN)的效率高。
組合索引字段的位置影響查詢效率,所以在建索引的時候需注意.
建議:把等值條件的字段放在組合索引的前導列,范圍字段放在第二列.