BitMap索引的效率要優於B-Tree索引執行個體
一、實驗說明:
作業系統:rhel 5.4 x86
資料庫:Oracle 11g R2
二、操作步驟:
首先建立一張t_btree表,並建立B-Tree索引,索引鍵是status:
SQL> create table t_btree as select * from dba_objects;
Table created.
SQL> create index status_btree on t_btree(status);
Index created.
執行兩次下面的查詢語句,並顯示執行計畫:
SQL> set autotrace traceonly;
SQL> select count(*) from t_btree where status='VALID';
Execution Plan
----------------------------------------------------------
Plan hash value: 2400455617
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 49 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
|* 2 | INDEX FAST FULL SCAN| STATUS_BTREE | 74307 | 362K| 49 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("STATUS"='VALID')
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
32 recursive calls
0 db block gets
261 consistent gets
458 physical reads
0 redo size
424 bytes sent via SQL*Net to client
419 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(*) from t_btree where status='VALID';
Execution Plan
----------------------------------------------------------
Plan hash value: 2400455617
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 49 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
|* 2 | INDEX FAST FULL SCAN| STATUS_BTREE | 74307 | 362K| 49 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("STATUS"='VALID')
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
180 consistent gets
0 physical reads
0 redo size
424 bytes sent via SQL*Net to client
419 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
接著建立跟t_btree一樣的表t_bmap,並建立BitMap索引。
SQL> create table t_bmap as select * from dba_objects;
Table created.
SQL> create bitmap index status_bmap on t_bmap(status);
Index created.
同樣執行之前的語句兩次:
SQL> select count(*) from t_bmap where status='VALID';
Execution Plan
----------------------------------------------------------
Plan hash value: 516980546
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
| 2 | BITMAP CONVERSION COUNT | | 62928 | 307K| 3 (0)| 00:00:01 |
|* 3 | BITMAP INDEX FAST FULL SCAN| STATUS_BMAP | | | | |
---------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("STATUS"='VALID')
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
32 recursive calls
0 db block gets
72 consistent gets
266 physical reads
0 redo size
424 bytes sent via SQL*Net to client
419 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(*) from t_bmap where status='VALID';
Execution Plan
----------------------------------------------------------
Plan hash value: 516980546
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
| 2 | BITMAP CONVERSION COUNT | | 62928 | 307K| 3 (0)| 00:00:01 |
|* 3 | BITMAP INDEX FAST FULL SCAN| STATUS_BMAP | | | | |
---------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("STATUS"='VALID')
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
6 consistent gets
0 physical reads
0 redo size
424 bytes sent via SQL*Net to client
419 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
從上面的查詢中,我們可以得到,分別給兩張內容一樣的表做查詢的時候,在執行第二次的時候是屬於軟解析:
從一致性讀上比較,B-Tree索引的consistent gets是180,BitMap的是6;
從Cost的消耗上看,B-Tree索引的COST是49,而BitMap的是3。
在索引鍵是高重複率索引值(status)的時候情況下BitMap索引的效率要優於B-Tree索引。
Oracle B樹索引簡介(B-Tree Index)
淺析Oracle B-Tree index搜尋原理
Oracle索引之B-Tree和Bitmap索引對比
從平衡樹到oracle B-Tree索引的原理探索