四種類型的Oracle索引掃描

根據索引的類型與where限制條件的不同,有4種類型的Oracle索引掃描:

  (1)       索引唯一掃描(index unique scan)

  (2)       索引範圍掃描(index range scan)

  (3)       索引全掃描(index full scan)

  (4)       索引快速掃描(index fast full scan)

  一. 索引唯一掃描(index unique scan)

  通過唯一索引查找一個數值經常返回單個ROWID。如果該唯一索引有多個列組成(即組合索引),則至少要有組合索引的引導列參與到該查詢中,如創建一個索引:create index idx_test on emp(ename, deptno, loc)。則select ename from emp where ename = ‘JACK’ and deptno = ‘DEV’語句可以使用該索引。如果該語句只返回一行,則存取方法稱爲索引唯一掃描。而select ename from emp where deptno = ‘DEV’語句則不會使用該索引,因爲where子句種沒有引導列。如果存在UNIQUE 或PRIMARY KEY 約束(它保證了語句只存取單行)的話,Oracle經常實現唯一性掃描。

  如:

  SQL> set autot traceonly exp;   -- 只顯示執行計劃

  SQL> select * from scott.emp t where t.empno=10;

  執行計劃

  ----------------------------------------------------------

  Plan hash value: 2949544139

  --------------------------------------------------------------------------------

  | Id | Operation                   | Name   | Rows | Bytes | Cost (%CPU)| Time

  --------------------------------------------------------------------------------

  |   0 | SELECT STATEMENT            |        |     1 |    38 |     1   (0)| 00:0

  |   1 | TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    38 |     1   (0)| 00:0

  |* 2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     0   (0)| 00:0

  --------------------------------------------------------------------------------

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  2 - access("T"."EMPNO"=10)

  二.索引範圍掃描(index range scan)

  使用一個索引存取多行數據,同上面一樣,如果索引是組合索引,而且select ename from emp where ename = ‘JACK’ and deptno = ‘DEV’語句返回多行數據,雖然該語句還是使用該組合索引進行查詢,可此時的存取方法稱爲索引範圍掃描。

  在唯一索引上使用索引範圍掃描的典型情況下是在謂詞(where限制條件)中使用了範圍操作符(如>、<、<>、>=、<=、between)

  使用索引範圍掃描的例子:

  SQL> select empno,ename from scott.emp where empno > 7876 order by empno;

  執行計劃

  ----------------------------------------------------------

  Plan hash value: 169057108

  --------------------------------------------------------------------------------

  | Id | Operation                   | Name   | Rows | Bytes | Cost (%CPU)| Time

  --------------------------------------------------------------------------------

  |   0 | SELECT STATEMENT            |        |     1 |    10 |     2   (0)| 00:0

  |   1 | TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    10 |     2   (0)| 00:0

  |* 2 |   INDEX RANGE SCAN          | PK_EMP |     1 |       |     1   (0)| 00:0

  --------------------------------------------------------------------------------

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  2 - access("EMPNO">7876)

  在非唯一索引上,謂詞可能返回多行數據,所以在非唯一索引上都使用索引範圍掃描。

  使用index rang scan的3種情況:

  (a) 在唯一索引列上使用了range操作符(> < <> >= <= between)。

  (b) 在組合索引上,只使用部分列進行查詢,導致查詢出多行。

  (c) 對非唯一索引列上進行的任何查詢。

  三.索引全掃描(index full scan)

  與全表掃描對應,也有相應的全Oracle索引掃描。在某些情況下,可能進行全Oracle索引掃描而不是範圍掃描,需要注意的是全Oracle索引掃描只在CBO模式下才有效。 CBO根據統計數值得知進行全Oracle索引掃描比進行全表掃描更有效時,才進行全Oracle索引掃描,而且此時查詢出的數據都必須從索引中可以直接得到。

  全Oracle索引掃描的例子:

  SQL> create index big_emp on scott.emp(empno,ename);

  索引已創建。

  SQL> select empno, ename from scott.emp order by empno,ename;

  執行計劃

  ----------------------------------------------------------

  Plan hash value: 322359667

  ----------------------------------------------------------------------------

  | Id | Operation        | Name    | Rows | Bytes | Cost (%CPU)| Time     |

  ----------------------------------------------------------------------------

  |   0 | SELECT STATEMENT |         |    14 |   140 |     1   (0)| 00:00:01 |

  |   1 | INDEX FULL SCAN | BIG_EMP |    14 |   140 |     1   (0)| 00:00:01 |

  ----------------------------------------------------------------------------

  四. 索引快速掃描(index fast full scan)

  掃描索引中的所有的數據塊,與 index full scan很類似,但是一個顯着的區別就是它不對查詢出的數據進行排序,即數據不是以排序順序被返回。在這種存取方法中,可以使用多塊讀功能,也可以使用並行讀入,以便獲得最大吞吐量與縮短執行時間。

  索引快速掃描的例子:

  SQL> select /*+ index_ffs(dave index_dave) */ id from dave where id>0;

  執行計劃

  ----------------------------------------------------------

  Plan hash value: 674200218

  --------------------------------------------------------------------------------

  | Id | Operation            | Name       | Rows | Bytes | Cost (%CPU)| Time

  --------------------------------------------------------------------------------

  |   0 | SELECT STATEMENT     |            |     8 |    24 |     2   (0)| 00:00:0

  |* 1 | INDEX FAST FULL SCAN| INDEX_DAVE |     8 |    24 |     2   (0)| 00:00:0

  --------------------------------------------------------------------------------

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  1 - filter("ID">0)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章