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

来源:互联网 发布:北京ps软件培训 编辑:程序博客网 时间:2024/05/01 03:17
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
使用索引误区之一没有使用复合索引前导列导致查询不使索引
在oracle中,我们经常以为建立了索引,sql查询的时候就会如我们所希望的那样使用索引,事实上,oracle只会在一定条件下使用索引,这里我们总结数第一点:oracle会在条件中包含了前导列时使用索引,即查询条件中必须使用索引中的第一个列,请看下面的例子

SQL>select*fromtab;

 

TNAME                         TABTYPE CLUSTERID

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

BONUS                         TABLE

DEPT                          TABLE

DUMMY                         TABLE

EMP                           TABLE

SALGRADE                      TABLE

 

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

SQL>createindexemp_id1onemp(empno,ename,deptno);

 

Indexcreated

 

建立一个单键索引

SQL>createindexemp_id2onemp(sal);

 

Indexcreated

 

 

SQL>selecttable_name,index_namefromuser_indexes

 2 wheretable_name='EMP';

 

TABLE_NAME                    INDEX_NAME

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

EMP                           EMP_ID1

EMP                           EMP_ID2

 

SQL>SELECT*FROMUSER_IND_COLUMNS

 2 /

 

INDEX_NAME                    TABLE_NAME                    COLUMN_NAME                                                                     COLUMN_POSITIONCOLUMN_LENGTHCHAR_LENGTHDESCEND

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------1<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击