深入理解Oracle索引(7):用实验数据观察从B-tree索引→复合索引→Bitmap索引所消费的CPU和I/O

环境:

sys@ORCL> !sqlplus -v

SQL*Plus: Release 10.2.0.1.0 - Production


sys@ORCL> !uname -a
Linux Think 2.6.18-308.el5xen #1 SMP Fri Jan 27 17:59:00 EST 2012 i686 i686 i386 GNU/Linux


hr@ORCL> conn sh/sh
Connected.
sh@ORCL> select index_name from user_indexes where table_name='CUSTOMERS';

INDEX_NAME
------------------------------
CUSTOMERS_MARITAL_BIX
CUSTOMERS_YOB_BIX
CUSTOMERS_PK
CUSTOMERS_GENDER_BIX

sh@ORCL> drop index CUSTOMERS_MARITAL_BIX;

Index dropped.

sh@ORCL> drop index CUSTOMERS_YOB_BIX;

Index dropped.

sh@ORCL> drop index CUSTOMERS_GENDER_BIX;

Index dropped.

sh@ORCL> drop index CUSTOMERS_PK;
drop index CUSTOMERS_PK
           *
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key


sh@ORCL> create index idx_cust_gender on customers (cust_gender);

Index created.

sh@ORCL> create index idx_cust_postal_code on customers (cust_postal_code);

Index created.

sh@ORCL> create index idx_cust_credit_limit on customers (cust_credit_limit);

Index created.
     
sh@ORCL> set autot trace

sh@ORCL> ed
Wrote file afiedt.buf

  1  select c.*
  2    from customers c
  3   where cust_gender = 'M' and
  4         cust_postal_code = '40804' and
  5*        cust_credit_limit = 10000
  6  

sh@ORCL> /

6 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 3555343875

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

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

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

|   0 | SELECT STATEMENT                 |                       |     6 |  1080 |    69   (3)| 00:0
0:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID     | CUSTOMERS             |     6 |  1080 |    69   (3)| 00:0
0:01 |

|   2 |   BITMAP CONVERSION TO ROWIDS    |                       |       |       |            |
     |

|   3 |    BITMAP AND                    |                       |       |       |            |
     |

|   4 |     BITMAP CONVERSION FROM ROWIDS|                       |       |       |            |
     |

|*  5 |      INDEX RANGE SCAN            | IDX_CUST_POSTAL_CODE  |    89 |       |     1   (0)| 00:0
0:01 |

|   6 |     BITMAP CONVERSION FROM ROWIDS|                       |       |       |            |
     |

|*  7 |      INDEX RANGE SCAN            | IDX_CUST_CREDIT_LIMIT |    89 |       |    14   (0)| 00:0
0:01 |

|   8 |     BITMAP CONVERSION FROM ROWIDS|                       |       |       |            |
     |

|*  9 |      INDEX RANGE SCAN            | IDX_CUST_GENDER       |    89 |       |    52   (2)| 00:0
0:01 |

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


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

   5 - access("CUST_POSTAL_CODE"='40804')
   7 - access("CUST_CREDIT_LIMIT"=10000)
   9 - access("CUST_GENDER"='M')


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         91  consistent gets
          0  physical reads
          0  redo size
       2974  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          6  rows processed

       Cost:205
       逻辑读:91


sh@ORCL> set autot off
sh@ORCL> select index_name from user_indexes where table_name='CUSTOMERS';

INDEX_NAME
------------------------------
IDX_CUST_GENDER
IDX_CUST_POSTAL_CODE
IDX_CUST_CREDIT_LIMIT
CUSTOMERS_PK

sh@ORCL> drop index IDX_CUST_GENDER;

Index dropped.

sh@ORCL> drop index IDX_CUST_POSTAL_CODE;

Index dropped.

sh@ORCL> drop index IDX_CUST_CREDIT_LIMIT;

Index dropped.

sh@ORCL> create index idx_cust_gen_pos_cred on customers (cust_gender,cust_postal_code,cust_credit_limit);

Index created.

sh@ORCL> set autot trace

sh@ORCL> ed
Wrote file afiedt.buf

  1  select c.*
  2    from customers c
  3   where cust_gender = 'M' and
  4         cust_postal_code = '40840' and
  5*        cust_credit_limit = 10000
  6  

sh@ORCL> /

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 3306790926

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

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

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

|   0 | SELECT STATEMENT            |                       |     6 |  1080 |     8   (0)| 00:00:01
|

|   1 |  TABLE ACCESS BY INDEX ROWID| CUSTOMERS             |     6 |  1080 |     8   (0)| 00:00:01
|

|*  2 |   INDEX RANGE SCAN          | IDX_CUST_GEN_POS_CRED |     7 |       |     1   (0)| 00:00:01
|

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


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

   2 - access("CUST_GENDER"='M' AND "CUST_POSTAL_CODE"='40840' AND "CUST_CREDIT_LIMIT"=10000)


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          2  consistent gets
          0  physical reads
          0  redo size
       1694  bytes sent via SQL*Net to client
        374  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

       Cost:17
       逻辑读:2


sh@ORCL> set autot off
sh@ORCL> select index_name from user_indexes where table_name='CUSTOMERS';

INDEX_NAME
------------------------------
IDX_CUST_GEN_POS_CRED
CUSTOMERS_PK

sh@ORCL> drop index IDX_CUST_GEN_POS_CRED;

Index dropped.

sh@ORCL> create bitmap index idx_cust_gender on customers(cust_gender);

Index created.

sh@ORCL> create bitmap index idx_cust_postal_code on customers (cust_postal_code);

Index created.

sh@ORCL> create bitmap index idx_cust_credit_limit on customers(cust_credit_limit);

Index created.

sh@ORCL> ed
Wrote file afiedt.buf

  1  select c.*
  2    from customers c
  3   where cust_gender = 'M' and
  4         cust_postal_code = '40804' and
  5*        cust_credit_limit = 10000
  6  

sh@ORCL> /

6 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 724066067

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

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

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

|   0 | SELECT STATEMENT             |                       |     6 |  1080 |     4   (0)| 00:00:01
 |

|   1 |  TABLE ACCESS BY INDEX ROWID | CUSTOMERS             |     6 |  1080 |     4   (0)| 00:00:01
 |

|   2 |   BITMAP CONVERSION TO ROWIDS|                       |       |       |            |
 |

|   3 |    BITMAP AND                |                       |       |       |            |
 |

|*  4 |     BITMAP INDEX SINGLE VALUE| IDX_CUST_POSTAL_CODE  |       |       |            |
 |

|*  5 |     BITMAP INDEX SINGLE VALUE| IDX_CUST_CREDIT_LIMIT |       |       |            |
 |

|*  6 |     BITMAP INDEX SINGLE VALUE| IDX_CUST_GENDER       |       |       |            |
 |

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


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

   4 - access("CUST_POSTAL_CODE"='40804')
   5 - access("CUST_CREDIT_LIMIT"=10000)
   6 - access("CUST_GENDER"='M')


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         15  consistent gets
          0  physical reads
          0  redo size
       2974  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          6  rows processed

       Cost:8
       逻辑读:15

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle 数据库中,索引可以分为以下几类: 1. B-Tree 索引B-Tree 索引是最常见的索引类型,它将索引键按照 B-Tree 结构有序存储,可以快速定位满足查询条件的记录。B-Tree 索引适用于等值查询、范围查询、排序等操作。 2. Bitmap 索引Bitmap 索引是一种非常适合高速查询低基数列的索引技术,它将每个索引键值映射到一个位图上,其中每个位表示一个行的存在或不存在。Bitmap 索引适用于位运算、多列联合查询等操作。 3. Function-Based 索引:Function-Based 索引是一种基于表达式的索引,它可以通过对列上的函数运算进行索引来提高查询性能。Function-Based 索引适用于需要对列进行函数运算的查询操作。 4. Cluster 索引:Cluster 索引是一种特殊的 B-Tree 索引,它将表的数据存储在与索引相同的 B-Tree 中,可以提高查询性能和数据访问效率,但是会增加插入和更新操作的成本。 5. Reverse 索引:Reverse 索引是一种特殊的 B-Tree 索引,它将索引键值按照相反的顺序存储,可以提高某些查询操作的性能。 6. Bitmap Join 索引Bitmap Join 索引是一种特殊的 Bitmap 索引,它可以用于加速表之间的连接操作,特别是大表的连接操作。 以上是 Oracle 数据库中常见的索引类型,根据实际情况和查询需求,可以选择合适的索引类型来提高查询性能和数据访问效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值