zoukankan      html  css  js  c++  java
  • oracle-组合索引字段位置与查询效率之间的关系

    Oracle索引组合字段的位置不同,当查询条件不能覆盖索引时,影响查询效率。查询条件是不是索引字段的第一列影响执行计划,实验验证

    实验1:查询条件为组合索引的第一列
    --
    创建测试表 create table test_C as select * from user_objects where 1=2; --创建测试表 create table test_a as select * from user_objects where 1=1; --创建组合索引 create index test_a_1 on test_a(object_id,object_type); set linesize 1000; set autotrace trace; select * from test_a where object_id=161728; **************************************************** 执行计划 ---------------------------------------------------------- Plan hash value: 3036018411 ------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 116 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST_A | 1 | 116 | 3 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | TEST_A_1 | 1 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=161728) 统计信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 3 consistent gets 0 physical reads 0 redo size 1933 bytes sent via SQL*Net to client 500 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed

    查询条件object_id为索引的前导列。执行计划:INDEX RANGE SCAN,cost是3,逻辑读是3.

    drop index test_a_1;
    drop index test_a_2;
    --创建索引2,查询条件在索引中的位置是第二列
    create index test_a_2 on test_a(object_name,object_ID);
    --再次查询
    select * from test_a where object_id=161728;
    ****************************************************
    执行计划
    ----------------------------------------------------------
    Plan hash value: 219338936
    
    ------------------------------------------------------------------------------------------------
    | Id  | Operation                           | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                    |          |     1 |   116 |     7   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| TEST_A   |     1 |   116 |     7   (0)| 00:00:01 |
    |*  2 |   INDEX SKIP SCAN                   | TEST_A_2 |     1 |       |     6   (0)| 00:00:01 |
    ------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("OBJECT_ID"=161728)
           filter("OBJECT_ID"=161728)
    
    
    统计信息
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
              8  consistent gets
              0  physical reads
              0  redo size
           1933  bytes sent via SQL*Net to client
            500  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processed

    查询条件object_id为索引的第二列。执行计划:INDEX SKIP SCAN,cost是7,逻辑读是8.

    同样object_id作为查询条件,当object_id作为索引的第一列或者作为索引的第二列,它们在cost与逻辑读上是有区别的,也就是索引跳跃扫描(INDEX SKIP SCAN)没有索引范围扫描(INDEX RANGE SCAN)的效率高。

    组合索引字段的位置影响查询效率,所以在建索引的时候需注意.

    建议:把等值条件的字段放在组合索引的前导列,范围字段放在第二列.

  • 相关阅读:
    openstack 使用cloud init 和 console-log, nbd或者libguestfs 获取VM中的硬件信息。
    Unity doesn't load, no Launcher, no Dash appears
    ssh 应用
    设计感悟——产品的3个属性
    别让用户发呆—设计中的防呆的6个策略
    用户流失原因调研4步经
    5种方法提高你网站的登录体验
    浅谈当下7个网页设计趋势(转)
    适应各浏览器图片裁剪无刷新上传jQuery插件(转)
    C#操作Excel数据增删改查(转)
  • 原文地址:https://www.cnblogs.com/handhead/p/13231617.html
Copyright © 2011-2022 走看看