index full scan和index fast full scan區別


 

觸發條件:只需要從索引中就可以取出所需要的結果集,此時就會走索引全掃描

Full Index Scan     按照數據的邏輯順序讀取數據塊,會發生單塊讀事件,
Fast Full Index Scan   按照數據塊的物理存儲位置順序讀取數據塊,會發生多塊讀事件,理論上索引快速全掃描會比索引全掃描要快

 

 

 

 

官檔的解釋:

Full Index Scan

In a full index scan, the database reads the entire index in order. A full index scan is available if a predicate (WHERE clause) in the SQL statement references a column in the index, and in some circumstances when no predicate is specified. A full scan can eliminate sorting because the data is ordered by index key.

原理:ORACLE定位到索引的ROOT BLOCK,然后到BRANCH BLOCK(如果有的話),再定位到第一個LEAF BLOCK, 然后根據LEAF BLOCK的雙向鏈表順序讀取。它所讀取的塊都是有順序的,也是經過排序的。

Fast Full Index Scan

A fast full index scan is a full index scan in which the database reads the index blocks in no particular order. The database accesses the data in the index itself, without accessing the table.

Fast full index scans are an alternative to full table scan when the index contains all the columns that are needed for the query, and at least one column in the index key has the NOT NULLconstraint.

A fast full scan is faster than a normal full index scan because it can use multiblock I/O and can run in parallel just like a table scan.

The database cannot perform fast full index scans of bitmap indexes.

原理:從段頭開始,讀取包含位圖塊,ROOT BLOCK,所有的BRANCH BLOCK,LEAF BLOCK,讀取的順序完全有物理存儲位置決定,並采取多塊讀,每次讀取DB_FILE_MULTIBLOCK_READ_COUNT個塊。查詢某個表記錄總數的時候,往往基於PRIMARY KEY的INDEX FAST FULL SCAN是最有效的。

 

測試COST:

SQL> create table t3 as select * from dba_objects;

表已創建。

SQL> create index t3_ix on t3(object_id);

索引已創建。

SQL> alter table t3 modify(object_id not null);

表已更改。

SQL> exec dbms_stats.gather_table_stats('SYS','T3');

PL/SQL 過程已成功完成。

SQL> set autot on
SQL> select count(*)  from t3;

  COUNT(*)
----------
     72006


執行計划
----------------------------------------------------------
Plan hash value: 271548554

------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |     1 |   162   (0)| 00:00:02 |
|   1 |  SORT AGGREGATE  |       |     1 |            |          |
|   2 |   INDEX FULL SCAN| T3_IX | 63127 |   162   (0)| 00:00:02 |
------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


SQL> select count(*)  from t3;

  COUNT(*)
----------
     72006


執行計划
----------------------------------------------------------
Plan hash value: 2285970227

-----------------------------------------------------------------------
| Id  | Operation             | Name  | Rows  | Cost (%CPU)| Time     |
-----------------------------------------------------------------------
|   0 | SELECT STATEMENT      |       |     1 |    62   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE       |       |     1 |            |          |
|   2 |   INDEX FAST FULL SCAN| T3_IX | 63127 |    62   (0)| 00:00:01 |
-----------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)

 

可以看到,indexl full scan的cost 為162,index fast full scan 的cost 為62,fast full scan的cost明顯要低。

 

測試SORT:

SQL> select object_id  from t3 where rownum<10 order by object_id;

 OBJECT_ID
----------
         2
         3
         4
         5
         6
         7
         8
         9
        10

已選擇9行。


執行計划
----------------------------------------------------------
Plan hash value: 633445068

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |     9 |    45 |     2   (0)| 00:00:01 |
|*  1 |  COUNT STOPKEY   |       |       |       |            |          |
|   2 |   INDEX FULL SCAN| T3_IX |     9 |    45 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(ROWNUM<10)

SQL> select object_id  from t3 where rownum<10 order by object_id;

 OBJECT_ID
----------
         2
         3
         4
         5
         6
         7
         8
         9
        10

已選擇9行。


執行計划
----------------------------------------------------------
Plan hash value: 512512221

----------------------------------------------------------------------------------------
| Id  | Operation              | Name  | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |       |     9 |    45 |       |   284   (2)| 00:00:04 |
|   1 |  SORT ORDER BY         |       |     9 |    45 |   856K|   284   (2)| 00:00:04 |
|*  2 |   COUNT STOPKEY        |       |       |       |       |            |          |
|   3 |    INDEX FAST FULL SCAN| T3_IX | 72006 |   351K|       |    61   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter(ROWNUM<10)

 

通過執行計划可以看到,fast_full_scan多了一個排序的步驟,而full scan沒有這個排序的步驟,說明full scan的數據是根據索引鍵排好序的,而fast_full_scan的多塊讀導致其沒有按照索引鍵排好序


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM