zoukankan      html  css  js  c++  java
  • 位图索引对单表查询效率的影响(使用解释计划评估)

    有这么一张表:

    create table hy_emp(
    id number(7,0) not null primary key,
    deptid number(2,0) not null,
    name nvarchar2(20) not null)

    这样给它充值:

    insert into hy_emp
    select rownum,dbms_random.value(1,10),dbms_random.string('*',dbms_random.value(3,18))
    from dual
    connect by level<=400000
    order by dbms_random.random

    然后查看以下SQL的解释计划:

    EXPLAIN PLAN FOR
    select * from hy_emp where deptid=10 and name='ZUVG'
    
    select * from table(dbms_xplan.display);

    解释计划如下:

    Plan hash value: 910676026
     
    ----------------------------------------------------------------------------
    | Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |        |    34 |  1632 |   548   (1)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| HY_EMP |    34 |  1632 |   548   (1)| 00:00:01 |
    ----------------------------------------------------------------------------
     
    Predicate Information (identified by operation id):
    ---------------------------------------------------
     
       1 - filter("DEPTID"=10 AND "NAME"=U'ZUVG')
     
    Note
    -----
       - dynamic statistics used: dynamic sampling (level=2)

    没有任何索引,自然是全表扫描,Cost是548.

    然后我们给deptid和name两个字段加个位图索引:

    create bitmap index hy_emp_idx_2 on hy_emp(deptid,name);

    再次查看解释计划:

    Plan hash value: 1386009635
     
    ----------------------------------------------------------------------------------------------------
    | Id  | Operation                           | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                    |              |     1 |    48 |    10   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| HY_EMP       |     1 |    48 |    10   (0)| 00:00:01 |
    |   2 |   BITMAP CONVERSION TO ROWIDS       |              |       |       |            |          |
    |*  3 |    BITMAP INDEX SINGLE VALUE        | HY_EMP_IDX_2 |       |       |            |          |
    ----------------------------------------------------------------------------------------------------
     
    Predicate Information (identified by operation id):
    ---------------------------------------------------
     
       3 - access("DEPTID"=10 AND "NAME"=U'ZUVG')
     
    Note
    -----
       - dynamic statistics used: dynamic sampling (level=2)

    Cost降到了10,因为新加的索引已经用上了。

    然后我们来验证全模糊查询是否能用上这个索引:

    EXPLAIN PLAN FOR
    select * from hy_emp where deptid=10 and name like '%ZUVG%'
    select * from table(dbms_xplan.display);

    解释计划如下:

    Plan hash value: 2088399487
     
    ----------------------------------------------------------------------------------------------------
    | Id  | Operation                           | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                    |              |    22 |  1056 |   501   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| HY_EMP       |    22 |  1056 |   501   (0)| 00:00:01 |
    |   2 |   BITMAP CONVERSION TO ROWIDS       |              |       |       |            |          |
    |*  3 |    BITMAP INDEX RANGE SCAN          | HY_EMP_IDX_2 |       |       |            |          |
    ----------------------------------------------------------------------------------------------------
     
    Predicate Information (identified by operation id):
    ---------------------------------------------------
     
       3 - access("DEPTID"=10)
           filter("NAME" LIKE U'%ZUVG%' AND "DEPTID"=10)
     
    Note
    -----
       - dynamic statistics used: dynamic sampling (level=2)

    从上面可以发现,新加的索引还是用上了,Cost也比全表查询要低一点,虽然低的不多。看来网上说的模糊查询会导致索引全失效的说法有点偏颇了,至少位图索引能起到聊胜于无的作用。

    --2020-03-17--

  • 相关阅读:
    [Java]用于将链表变成字符串并在元素之间插入分隔符的有用函数“String.join”
    Sql语法树示例 select username, ismale from userinfo where age > 20 and level > 5 and 1 = 1
    [Java]一段尚未雕琢的分词代码
    day44_Oracle学习笔记_03
    day43_Oracle学习笔记_02
    WinXP系统中的Oracle数据库如何以管理员身份登录
    Oracle 10G安装指导
    20个Linux服务器性能调优技巧
    Linux 上使用 Gmail SMTP 服务器发送邮件通知
    Netdata Linux下性能实时监测工具
  • 原文地址:https://www.cnblogs.com/heyang78/p/12511171.html
Copyright © 2011-2022 走看看