温馨提示×

温馨提示×

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

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

oracle在组合索引上,只使用部分列进行查询(查询时必须包含前导列,否则会走全表扫描)

发布时间:2020-07-07 20:17:37 来源:网络 阅读:1447 作者:corasql 栏目:关系型数据库

实验环境:Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

1、创建表插入数据

SQL> create table txtx(id int,name char(2),tx char(3),id1 int,primary key(id,name,tx));
表已创建。
SQL> insert into txtx values(1,'tx','tx',1);
已创建 1 行。
SQL> insert into txtx values(2,'tx','tx',2);
已创建 1 行。
SQL> insert into txtx values(3,'tx','tx',3);
已创建 1 行。
SQL> commit;
SQL> select * from txtx;
        ID NA TX         ID1
---------- -- --- ----------
         1 tx tx           1
         2 tx tx           2
         3 tx tx           3

2、执行计划

SQL> explain plan for select * from txtx  where id=1 and id1 =1 and tx='tx';
已解释。
SQL> set linesize 200
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 4191381592
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    35 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TXTX |     1 |    35 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   1 - filter("ID"=1 AND "ID1"=1 AND "TX"='tx')
Note
-----
   - dynamic sampling used for this statement (level=2)
已选择17行。

通过以上执行计划,可以看出,不含前导列,进行了全表扫描,以下使用了前导列,查询速度就上来了

SQL> explain plan for select * from txtx  where id=1 and name ='tx' and tx='tx';
已解释。
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 913771524
--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |     1 |    35 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TXTX         |     1 |    35 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | SYS_C0024000 |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------
   2 - access("ID"=1 AND "NAME"='tx' AND "TX"='tx')
已选择14行。


向AI问一下细节

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

AI