• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

使用索引的误区之一:没有使用复合索引的前导列导致查询不使用索引

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 122次 | 进入软件测试论坛讨论

领测软件测试网 使用索引的误区之一:没有使用复合索引的前导列导致查询不使用索引
在oracle中,我们经常以为建立了索引,sql查询的时候就会如我们所希望的那样使用索引,事实上,oracle只会在一定条件下使用索引,这里我们总结数第一点:oracle会在条件中包含了前导列时使用索引,即查询条件中必须使用索引中的第一个列,请看下面的例子

SQL> select * from tab;

 

TNAME                          TABTYPE  CLUSTERID

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

BONUS                          TABLE

DEPT                           TABLE

DUMMY                          TABLE

EMP                            TABLE

SALGRADE                       TABLE

 

建立一个联合索引(注意复合索引的索引列顺序)

SQL> create index emp_id1 on emp(empno,ename,deptno);

 

Index created

 

建立一个单键索引

SQL> create index emp_id2 on emp(sal);

 

Index created

 

 

SQL> select table_name,index_name from user_indexes

  2  where table_name=@#EMP@#;

 

TABLE_NAME                     INDEX_NAME

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

EMP                            EMP_ID1

EMP                            EMP_ID2

 

SQL> SELECT * FROM USER_IND_COLUMNS

  2  /

 

INDEX_NAME                     TABLE_NAME                     COLUMN_NAME                                                                      COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESCEND

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

EMP_ID1                        EMP                            EMPNO                                                                                          1            22           0 ASC

EMP_ID1                        EMP                            ENAME                                                                                          2            10          10 ASC

EMP_ID1                        EMP                            DEPTNO                                                                                         3            22           0 ASC

EMP_ID2                        EMP                            SAL                                                                                            1            22           0 ASC

 

下面的查询由于没有使用到复合索引的前导列,所以没有使用索引

 select job, empno from emp where ename=@#RICH@#;

 

PLAN_TABLE_OUTPUT

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

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

| Id  | Operation            |  Name       | Rows  | Bytes | Cost  |

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

|   0 | SELECT STATEMENT     |             |       |       |       |

|*  1 |  TABLE ACCESS FULL   | EMP         |       |       |       |

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

Predicate Information (identified by operation id):

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

   1 - filter("EMP"."ENAME"=@#RICH@#)

Note: rule based optimization

 

14 rows selected

 

 

下面的查询也由于没有使用到复合索引的前导列,所以没有使用索引

select job, empno from emp where deptno=30;

 

PLAN_TABLE_OUTPUT

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

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

| Id  | Operation            |  Name       | Rows  | Bytes | Cost  |

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

|   0 | SELECT STATEMENT     |             |       |       |       |

|*  1 |  TABLE ACCESS FULL   | EMP         |       |       |       |

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

Predicate Information (identified by operation id):

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

   1 - filter("EMP"."DEPTNO"=30)

Note: rule based optimization

 

14 rows selected

 

 

 

下面的查询使用了复合索引中的前导列,所以查询走索引了

select job, empno from emp where empno=7777;

 

PLAN_TABLE_OUTPUT

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

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

| Id  | Operation                   |  Name       | Rows  | Bytes | Cost  |

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

|   0 | SELECT STATEMENT            |             |       |       |       |

|   1 |  TABLE ACCESS BY INDEX ROWID| EMP         |       |       |       |

|*  2 |   INDEX RANGE SCAN          | EMP_ID1     |       |       |       |

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

Predicate Information (identified by operation id):

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

   2 - access("EMP"."EMPNO"=7777)

Note: rule based optimization

 

15 rows selected

 

 

 

 

下面的查询使用了复合索引中的第一列和第二列,所以查询走索引了

select job, empno from emp where empno=7777 and ename=@#RICH@#;

 

PLAN_TABLE_OUTPUT

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

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

| Id  | Operation                   |  Name       | Rows  | Bytes | Cost  |

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

|   0 | SELECT STATEMENT            |             |       |       |       |

|   1 |  TABLE ACCESS BY INDEX ROWID| EMP         |       |       |       |

|*  2 |   INDEX RANGE SCAN          | EMP_ID1     |       |       |       |

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

Predicate Information (identified by operation id):

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

   2 - access("EMP"."EMPNO"=7777 AND "EMP"."ENAME"=@#RICH@#)

Note: rule based optimization

 

15 rows selected

 

 

 

使用了复合索引的全部列,所以走索引了,另外由于选了了索引中没有包含的列(job),

所以进行索引全表扫描得到满足条件的rowid后,还要到表中检索相应的行

select job, empno from emp where empno=7777 and ename=@#RICH@# and deptno=30;

 

PLAN_TABLE_OUTPUT

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

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

| Id  | Operation                   |  Name       | Rows  | Bytes | Cost  |

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

|   0 | SELECT STATEMENT            |             |       |       |       |

|   1 |  TABLE ACCESS BY INDEX ROWID| EMP         |       |       |       |

|*  2 |   INDEX RANGE SCAN          | EMP_ID1     |       |       |       |

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

Predicate Information (identified by operation id):

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

   2 - access("EMP"."EMPNO"=7777 AND "EMP"."ENAME"=@#RICH@# AND "EMP"."DEP

              TNO"=30)

Note: rule based optimization

 

16 rows selected

 

 

 

 

使用了复合索引的全部列,所以走索引了,而且由于所有选择的列都包含在索引中,所以仅仅进行了索引范围扫描

select empno from emp where empno=7777 and ename=@#RICH@# and deptno=30;

 

PLAN_TABLE_OUTPUT

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

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

| Id  | Operation            |  Name       | Rows  | Bytes | Cost  |

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

|   0 | SELECT STATEMENT     |             |       |       |       |

|*  1 |  INDEX RANGE SCAN    | EMP_ID1     |       |       |       |

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

Predicate Information (identified by operation id):

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

   1 - access("EMP"."EMPNO"=7777 AND "EMP"."ENAME"=@#RICH@# AND "EM

              P"."DEPTNO"=30)

Note: rule based optimization

 

15 rows selected

 

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网