索引优化系列十五--组合查询和in有关的优化

发布时间:2020-07-07 12:23:57 作者:1415699306
来源:网络 阅读:345

drop table t purge;

create table t as select * from dba_objects;

update t set object_id=rownum ;

create index idx_id_type on t(object_id,object_type);


UPDATE t SET OBJECT_ID=20 WHERE ROWNUM<=26000;

UPDATE t SET OBJECT_ID=21 WHERE OBJECT_ID<>20;

COMMIT;

set linesize 1000

set pagesize 1

alter session set statistics_level=all ;


select  /*+index(t,idx1_object_id)*/ * from t  where object_TYPE='TABLE'  AND OBJECT_ID >= 20 AND OBJECT_ID<= 21;

select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

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

| Id  | Operation                   | Name           | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

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

|   0 | SELECT STATEMENT            |                |      1 |        |   2939 |00:00:00.02 |    1117 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T              |      1 |   3411 |   2939 |00:00:00.02 |    1117 |

|*  2 |   INDEX RANGE SCAN          | IDX_ID_TYPE    |      1 |    299 |   2939 |00:00:00.02 |     736 |

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

2 - access("OBJECT_ID">=20 AND "OBJECT_TYPE"='TABLE' AND "OBJECT_ID"<=21)

    filter("OBJECT_TYPE"='TABLE')

已选择25行。







select  /*+index(t,idx_id_type)*/ * from t t where object_TYPE='TABLE'  AND  OBJECT_ID IN (20,21);

select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

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

| Id  | Operation                    | Name           | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

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

|   0 | SELECT STATEMENT             |                |      1 |        |   2939 |00:00:00.01 |     598 |

|   1 |  INLIST ITERATOR             |                |      1 |        |   2939 |00:00:00.01 |     598 |

|   2 |   TABLE ACCESS BY INDEX ROWID| T              |      2 |   3411 |   2939 |00:00:00.01 |     598 |

|*  3 |    INDEX RANGE SCAN          | IDX_ID_TYPE    |      2 |      1 |   2939 |00:00:00.01 |     217 |

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

3 - access((("OBJECT_ID"=20 OR "OBJECT_ID"=21)) AND "OBJECT_TYPE"='TABLE')

已选择25行。




---col1,col2,col3的索引情况,如果没有为COL2赋予查询条件时,COL3只能起到检验作用(依然是in的优化)


drop table t purge;

create table t as select * from dba_objects;

UPDATE t SET OBJECT_ID=20 WHERE ROWNUM<=26000;

UPDATE t SET OBJECT_ID=21 WHERE OBJECT_ID<>20;

Update t set object_id=22 where rownum<=10000;

COMMIT;


create index idx_union on t(object_type,object_id,owner);

set autotrace off

alter session set statistics_level=all ;

set linesize 1000

select * from t where object_type='VIEW' and OWNER='LJB';

select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

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

| Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |

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

|   0 | SELECT STATEMENT            |           |      1 |        |      4 |00:00:00.01 |      24 |  19 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T         |      1 |      9 |      4 |00:00:00.01 |      24 |  19 |

|*  2 |   INDEX RANGE SCAN          | IDX_UNION |      1 |     22 |      4 |00:00:00.01 |      21 |  19 |

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


select /*+INDEX(T,idx_union)*/ * from t T where object_type='VIEW' and OBJECT_ID IN (20,21,22) AND OWNER='LJB';

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

| Id  | Operation                    | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

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

|   0 | SELECT STATEMENT             |           |      1 |        |      4 |00:00:00.01 |      13 |

|   1 |  INLIST ITERATOR             |           |      1 |        |      4 |00:00:00.01 |      13 |

|   2 |   TABLE ACCESS BY INDEX ROWID| T         |      3 |      1 |      4 |00:00:00.01 |      13 |

|*  3 |    INDEX RANGE SCAN          | IDX_UNION |      3 |      1 |      4 |00:00:00.01 |      10 |

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


类似

select /*+INDEX(T,idx_union)*/ * from t T where (object_type='VIEW' and OBJECT_ID =20 AND OWNER='LJB')

                                                or (object_type='VIEW' and OBJECT_ID =21 AND OWNER='LJB')

                                                or (object_type='VIEW' and OBJECT_ID =22 AND OWNER='LJB')


推荐阅读:
  1. mongodb查询与索引优化
  2. MYSQL性能故障优化利器之索引优化

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oracle --

上一篇:wamp设置(多站点配置,自动启动)

下一篇:Android style 的继承

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》