用示例说明BitMap索引的效率要优于B-Tree索引
生活随笔
收集整理的這篇文章主要介紹了
用示例说明BitMap索引的效率要优于B-Tree索引
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實驗說明:
???? 操作系統:rhel 5.4 x86
???? 數據庫:Oracle 11g R2
二、操作步驟:
??? 首先創建一張t_btree表,并建立B-Tree索引,索引鍵是status:
1 SQL> create table t_btree as select * from dba_objects; 2 3 Table created. 4 5 SQL> create index status_btree on t_btree(status); 6 7 Index created.?? 執行兩次下面的查詢語句,并顯示執行計劃:
1 SQL> set autotrace traceonly; 2 SQL> select count(*) from t_btree where status='VALID'; 3 4 5 Execution Plan 6 ---------------------------------------------------------- 7 Plan hash value: 2400455617 8 9 -------------------------------------------------------------------------------------- 10 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 11 -------------------------------------------------------------------------------------- 12 | 0 | SELECT STATEMENT | | 1 | 5 | 49 (0)| 00:00:01 | 13 | 1 | SORT AGGREGATE | | 1 | 5 | | | 14 |* 2 | INDEX FAST FULL SCAN| STATUS_BTREE | 74307 | 362K| 49 (0)| 00:00:01 | 15 -------------------------------------------------------------------------------------- 16 17 Predicate Information (identified by operation id): 18 --------------------------------------------------- 19 20 2 - filter("STATUS"='VALID') 21 22 Note 23 ----- 24 - dynamic sampling used for this statement (level=2) 25 26 27 Statistics 28 ---------------------------------------------------------- 29 32 recursive calls 30 0 db block gets 31 261 consistent gets 32 458 physical reads 33 0 redo size 34 424 bytes sent via SQL*Net to client 35 419 bytes received via SQL*Net from client 36 2 SQL*Net roundtrips to/from client 37 0 sorts (memory) 38 0 sorts (disk) 39 1 rows processed 40 41 SQL> select count(*) from t_btree where status='VALID'; 42 43 44 Execution Plan 45 ---------------------------------------------------------- 46 Plan hash value: 2400455617 47 48 -------------------------------------------------------------------------------------- 49 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 50 -------------------------------------------------------------------------------------- 51 | 0 | SELECT STATEMENT | | 1 | 5 | 49 (0)| 00:00:01 | 52 | 1 | SORT AGGREGATE | | 1 | 5 | | | 53 |* 2 | INDEX FAST FULL SCAN| STATUS_BTREE | 74307 | 362K| 49 (0)| 00:00:01 | 54 -------------------------------------------------------------------------------------- 55 56 Predicate Information (identified by operation id): 57 --------------------------------------------------- 58 59 2 - filter("STATUS"='VALID') 60 61 Note 62 ----- 63 - dynamic sampling used for this statement (level=2) 64 65 66 Statistics 67 ---------------------------------------------------------- 68 0 recursive calls 69 0 db block gets 70 180 consistent gets 71 0 physical reads 72 0 redo size 73 424 bytes sent via SQL*Net to client 74 419 bytes received via SQL*Net from client 75 2 SQL*Net roundtrips to/from client 76 0 sorts (memory) 77 0 sorts (disk) 78 1 rows processed?? 接著創建跟t_btree一樣的表t_bmap,并創建BitMap索引。
1 SQL> create table t_bmap as select * from dba_objects; 2 3 Table created. 4 5 SQL> create bitmap index status_bmap on t_bmap(status); 6 7 Index created.?? 同樣執行之前的語句兩次:
1 SQL> select count(*) from t_bmap where status='VALID'; 2 3 4 Execution Plan 5 ---------------------------------------------------------- 6 Plan hash value: 516980546 7 8 --------------------------------------------------------------------------------------------- 9 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 10 --------------------------------------------------------------------------------------------- 11 | 0 | SELECT STATEMENT | | 1 | 5 | 3 (0)| 00:00:01 | 12 | 1 | SORT AGGREGATE | | 1 | 5 | | | 13 | 2 | BITMAP CONVERSION COUNT | | 62928 | 307K| 3 (0)| 00:00:01 | 14 |* 3 | BITMAP INDEX FAST FULL SCAN| STATUS_BMAP | | | | | 15 --------------------------------------------------------------------------------------------- 16 17 Predicate Information (identified by operation id): 18 --------------------------------------------------- 19 20 3 - filter("STATUS"='VALID') 21 22 Note 23 ----- 24 - dynamic sampling used for this statement (level=2) 25 26 27 Statistics 28 ---------------------------------------------------------- 29 32 recursive calls 30 0 db block gets 31 72 consistent gets 32 266 physical reads 33 0 redo size 34 424 bytes sent via SQL*Net to client 35 419 bytes received via SQL*Net from client 36 2 SQL*Net roundtrips to/from client 37 0 sorts (memory) 38 0 sorts (disk) 39 1 rows processed 40 41 SQL> select count(*) from t_bmap where status='VALID'; 42 43 44 Execution Plan 45 ---------------------------------------------------------- 46 Plan hash value: 516980546 47 48 --------------------------------------------------------------------------------------------- 49 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 50 --------------------------------------------------------------------------------------------- 51 | 0 | SELECT STATEMENT | | 1 | 5 | 3 (0)| 00:00:01 | 52 | 1 | SORT AGGREGATE | | 1 | 5 | | | 53 | 2 | BITMAP CONVERSION COUNT | | 62928 | 307K| 3 (0)| 00:00:01 | 54 |* 3 | BITMAP INDEX FAST FULL SCAN| STATUS_BMAP | | | | | 55 --------------------------------------------------------------------------------------------- 56 57 Predicate Information (identified by operation id): 58 --------------------------------------------------- 59 60 3 - filter("STATUS"='VALID') 61 62 Note 63 ----- 64 - dynamic sampling used for this statement (level=2) 65 66 67 Statistics 68 ---------------------------------------------------------- 69 0 recursive calls 70 0 db block gets 71 6 consistent gets 72 0 physical reads 73 0 redo size 74 424 bytes sent via SQL*Net to client 75 419 bytes received via SQL*Net from client 76 2 SQL*Net roundtrips to/from client 77 0 sorts (memory) 78 0 sorts (disk) 79 1 rows processed?? 從上面的查詢中,我們可以得到,分別給兩張內容一樣的表做查詢的時候,在執行第二次的時候是屬于軟解析:
?? 從一致性讀上比較,B-Tree索引的consistent gets是180,BitMap的是6;
?? 從Cost的消耗上看,B-Tree索引的COST是49,而BitMap的是3。
?? 在索引鍵是高重復率鍵值(status)的時候情況下BitMap索引的效率要優于B-Tree索引。
轉載于:https://www.cnblogs.com/Richardzhu/archive/2012/12/24/2831044.html
總結
以上是生活随笔為你收集整理的用示例说明BitMap索引的效率要优于B-Tree索引的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++排序算法ppt_C/C++学习教程
- 下一篇: 英特尔固态硬盘测试软件,Intel SS