zoukankan      html  css  js  c++  java
  • 用示例说明BTree索引性能优于BitMap索引

    一、实验说明:

         操作系统:rhel 5.4 x86

         数据库:Oracle 11g R2

         实验说明:该实验是为了说明B-Tree索引性能优于BitMap索引的情况。

     

    二、实验操作:

      首先创建一张t_btree表,并建立B-Tree索引,索引键是object_id:

    1 SQL> create table t_btree as select * from dba_objects;
    2 
    3 Table created.
    4 
    5 SQL> create index ind_tree on t_btree(object_id);
    6 
    7 Index created.

      执行两次下面的查询语句,并显示执行计划:

     1 SQL> set autotrace traceonly;
     2 SQL> select * from t_btree where object_id=9899;
     3 
     4 
     5 Execution Plan
     6 ----------------------------------------------------------
     7 Plan hash value: 447474086
     8 
     9 ----------------------------------------------------------------------------------------
    10 | Id  | Operation            | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    11 ----------------------------------------------------------------------------------------
    12 |   0 | SELECT STATEMENT        |           |     1 |   207 |     2     (0)| 00:00:01 |
    13 |   1 |  TABLE ACCESS BY INDEX ROWID| T_BTREE  |     1 |   207 |     2     (0)| 00:00:01 |
    14 |*  2 |   INDEX RANGE SCAN        | IND_TREE |     1 |       |     1     (0)| 00:00:01 |
    15 ----------------------------------------------------------------------------------------
    16 
    17 Predicate Information (identified by operation id):
    18 ---------------------------------------------------
    19 
    20    2 - access("OBJECT_ID"=9899)
    21 
    22 Note
    23 -----
    24    - dynamic sampling used for this statement (level=2)
    25 
    26 
    27 Statistics
    28 ----------------------------------------------------------
    29     312  recursive calls
    30       0  db block gets
    31     108  consistent gets
    32     289  physical reads
    33       0  redo size
    34    1404  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 * from t_btree where object_id=9899;
    42 
    43 
    44 Execution Plan
    45 ----------------------------------------------------------
    46 Plan hash value: 447474086
    47 
    48 ----------------------------------------------------------------------------------------
    49 | Id  | Operation            | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    50 ----------------------------------------------------------------------------------------
    51 |   0 | SELECT STATEMENT        |           |     1 |   207 |     2     (0)| 00:00:01 |
    52 |   1 |  TABLE ACCESS BY INDEX ROWID| T_BTREE  |     1 |   207 |     2     (0)| 00:00:01 |
    53 |*  2 |   INDEX RANGE SCAN        | IND_TREE |     1 |       |     1     (0)| 00:00:01 |
    54 ----------------------------------------------------------------------------------------
    55 
    56 Predicate Information (identified by operation id):
    57 ---------------------------------------------------
    58 
    59    2 - access("OBJECT_ID"=9899)
    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       4  consistent gets
    71       0  physical reads
    72       0  redo size
    73    1404  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 ind_map on t_bmap(object_id);
    6 
    7 Index created.

      同样执行之前的语句两次:

     1 SQL>  select * from t_bmap where object_id=9899; 
     2 
     3 
     4 Execution Plan
     5 ----------------------------------------------------------
     6 Plan hash value: 3763176822
     7 
     8 ----------------------------------------------------------------------------------------
     9 | Id  | Operation             | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    10 ----------------------------------------------------------------------------------------
    11 |   0 | SELECT STATEMENT         |           |     1 |   207 |   110     (0)| 00:00:02 |
    12 |   1 |  TABLE ACCESS BY INDEX ROWID | T_BMAP  |     1 |   207 |   110     (0)| 00:00:02 |
    13 |   2 |   BITMAP CONVERSION TO ROWIDS|           |       |       |        |           |
    14 |*  3 |    BITMAP INDEX SINGLE VALUE | IND_MAP |       |       |        |           |
    15 ----------------------------------------------------------------------------------------
    16 
    17 Predicate Information (identified by operation id):
    18 ---------------------------------------------------
    19 
    20    3 - access("OBJECT_ID"=9899)
    21 
    22 Note
    23 -----
    24    - dynamic sampling used for this statement (level=2)
    25 
    26 
    27 Statistics
    28 ----------------------------------------------------------
    29     312  recursive calls
    30       0  db block gets
    31      98  consistent gets
    32     266  physical reads
    33       0  redo size
    34    1404  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 * from t_bmap where object_id=9899; 
    42 
    43 
    44 Execution Plan
    45 ----------------------------------------------------------
    46 Plan hash value: 3763176822
    47 
    48 ----------------------------------------------------------------------------------------
    49 | Id  | Operation             | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    50 ----------------------------------------------------------------------------------------
    51 |   0 | SELECT STATEMENT         |           |     1 |   207 |   110     (0)| 00:00:02 |
    52 |   1 |  TABLE ACCESS BY INDEX ROWID | T_BMAP  |     1 |   207 |   110     (0)| 00:00:02 |
    53 |   2 |   BITMAP CONVERSION TO ROWIDS|           |       |       |        |           |
    54 |*  3 |    BITMAP INDEX SINGLE VALUE | IND_MAP |       |       |        |           |
    55 ----------------------------------------------------------------------------------------
    56 
    57 Predicate Information (identified by operation id):
    58 ---------------------------------------------------
    59 
    60    3 - access("OBJECT_ID"=9899)
    61 
    62 Note
    63 -----
    64    - dynamic sampling used for this statement (level=2)
    65 
    66 
    67 Statistics
    68 ----------------------------------------------------------
    69       7  recursive calls
    70       0  db block gets
    71      68  consistent gets
    72       0  physical reads
    73       0  redo size
    74    1404  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是4,BitMap的是68;

       从Cost的消耗上看,B-Tree索引的COST是2,而BitMap的是110。

       在索引键是主键或者唯一性约束的情况下B-Tree索引的效率要优于BitMap索引。

  • 相关阅读:
    动态表单之数据分页
    SQL Server 批量生成bcp命令
    SQL Server 全文索引的硬伤
    简单实用SQL脚本Part2:日期和时间函数
    使用SQL Server 扩展函数进行性能优化
    留念2010年5月5日
    C#获取URL参数值
    SQL Server扩展函数的基本概念
    SQL Server 空间换时间的数据库设计
    简单实用SQL脚本Part:查找SQL Server 自增ID值不连续记录
  • 原文地址:https://www.cnblogs.com/Richardzhu/p/2830923.html
Copyright © 2011-2022 走看看