使用索引的误区之六:为索引列都建立索引

来源:互联网 发布:北京ps软件培训 编辑:程序博客网 时间:2024/04/30 21:02
<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>
使用索引误区之六:为所有列都建立索引
我们知道,建立索引是为了提高查询的效率,但是同时也应该注意到,索引增加了对DML操作(insert,update,delete)的代价,而且,一给中的索引如果太多,那么多数的索引是根本不会被使用到的,而另一方面我们维护这些不被使用的所以还要大幅度降低系统的性能。所以,索引不是越多越好,而是要恰到好处的使用。

 

比如说,有些列由于使用了函数,我们要使用已有的索引(如一些复合索引)是不可能的(详细请参见前面“函数索引”),那么就必须建立单独的函数索引,如果说这个函数索引很少会被应用(仅仅在几个特别的sql中会用到),我们就可以尝试改写查询,而不去建立和维护那个函数索引,例如:

C:/>sqlplusdemo/demo

 

SQL*Plus:Release9.2.0.1.0-ProductiononSunOct1707:47:302004

 

Copyright(c)1982,2002,Corporation. Allrightsreserved.

 

 

Connectedto:

Oracle9iEnterpriseEditionRelease9.2.0.1.0-Production

WiththeOLAPandOracleDataMiningoptions

JServerRelease9.2.0.1.0-Production

 

SQL>showuser

Useris"demo"

 

SQL>descemp

Name    Type        NullableDefaultComments

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

EMPNO   NUMBER(4)                             

ENAME   VARCHAR2(10)Y                        

JOB     VARCHAR2(9) Y                        

MGR     NUMBER(4)   Y                        

HIREDATEDATE        Y                        

SAL     NUMBER(7,2) Y                        

COMM    NUMBER(7,2) Y                        

DEPTNO  NUMBER(2)   Y                        

 

SQL>createindexemp_id3onemp(hiredate);

 

Indexcreated

 

 

1,trunc函数

SQL>selectempno,ename,deptnofromempwheretrunc(hiredate)='2004-01-01';

 

norowsselected

 

 

ExecutionPlan

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

  0     SELECTSTATEMENTOptimizer=CHOOSE共5页  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>
原创粉丝点击