首页 > 开发 > 综合 > 正文

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

2024-07-21 02:06:29
字体:
来源:转载
供稿:网友
使用索引的误区之六:为所有列都建立索引
我们知道,建立索引是为了提高查询的效率,但是同时也应该注意到,索引增加了对dml操作(insert, update, delete)的代价,而且,一给中的索引如果太多,那么多数的索引是根本不会被使用到的,而另一方面我们维护这些不被使用的所以还要大幅度降低系统的性能。所以,索引不是越多越好,而是要恰到好处的使用。

 

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

c:/>sqlplus demo/demo

 

sql*plus: release 9.2.0.1.0 - production on sun oct 17 07:47:30 2004

 

copyright (c) 1982, 2002, oracle corporation.  all rights reserved.

 

 

connected to:

oracle9i enterprise edition release 9.2.0.1.0 - production

with the olap and oracle data mining options

jserver release 9.2.0.1.0 - production

 

sql> show user

user is "demo"

 

sql> desc emp

name     type         nullable default comments

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

empno    number(4)                             

ename    varchar2(10) y                         

job      varchar2(9)  y                        

mgr      number(4)    y                        

hiredate date         y                        

sal      number(7,2)  y                        

comm     number(7,2)  y                         

deptno   number(2)    y                        

 

sql> create index emp_id3 on emp(hiredate);

 

index created

 

 

1,trunc函数

sql> select empno,ename,deptno from emp where trunc(hiredate)='2004-01-01';

 

no rows selected

 

 

execution plan

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

   0      select statement optimizer=choose

   1    0   table access (full) of 'emp'

 

 

 

 

statistics

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

          0  recursive calls

          0  db block gets

          3  consistent gets

          0  physical reads

          0  redo size

        328  bytes sent via sql*net to client

        372  bytes received via sql*net from client

          1  sql*net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          0  rows processed

 

将上面的查询转换为:

sql> select empno,ename,deptno from emp

  2  where hiredate >= to_date('2004-01-01','yyyy-mm-dd')

  3  and hiredate<to_date('2004-01-01','yyyy-mm-dd')+0.999;

 

no rows selected

 

 

execution plan

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

   0      select statement optimizer=choose

   1    0   table access (by index rowid) of 'emp'

   2    1     index (range scan) of 'emp_id3' (non-unique)

 

 

 

 

statistics

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

          0  recursive calls

          0  db block gets

          1  consistent gets

          0  physical reads

          0  redo size

        328  bytes sent via sql*net to client

        372  bytes received via sql*net from client

          1  sql*net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          0  rows processed

 

sql>

 

 

2,to_char函数

sql> select empno,ename,deptno from emp

  2  where to_char(hiredate,'yyyy-mm-dd')='2003-09-05';

 

no rows selected

 

 

execution plan

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

   0      select statement optimizer=choose

   1    0   table access (full) of 'emp'

 

 

 

 

statistics

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

          0  recursive calls

          0  db block gets

          3  consistent gets

          0  physical reads

          0  redo size

        328  bytes sent via sql*net to client

        372  bytes received via sql*net from client

          1  sql*net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          0  rows processed

 

sql> select empno,ename,deptno from emp

  2  where hiredate=to_date('2003-09-05','yyyy-mm-dd');

 

no rows selected

 

 

execution plan

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

   0      select statement optimizer=choose

   1    0   table access (by index rowid) of 'emp'

   2    1     index (range scan) of 'emp_id3' (non-unique)

 

 

 

 

statistics

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

          0  recursive calls

          0  db block gets

          1  consistent gets

          0  physical reads

          0  redo size

        328  bytes sent via sql*net to client

        372  bytes received via sql*net from client

          1  sql*net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          0  rows processed

 

sql>

注意,转换时注意数据库中对字段的精度,如'yyyymmyy',或者'yyyymmddhh24miss'

 

3,to_date函数

参见上面的方法

 

 

4,substr函数

sql> desc dept

 name                                      null?    type

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

 deptno                                             number(2)

 dname                                              varchar2(14)

 loc                                                varchar2(13)

 

sql> create index dept_id1 on dept(dname);

 

index created.

 

sql> select dname from dept where substr(dname,1,3)='abc';

 

no rows selected

 

 

execution plan

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

   0      select statement optimizer=choose

   1    0   table access (full) of 'dept'

 

 

 

 

statistics

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

          0  recursive calls

          0  db block gets

          7  consistent gets

          0  physical reads

          0  redo size

        221  bytes sent via sql*net to client

        372  bytes received via sql*net from client

          1  sql*net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          0  rows processed

 

sql>  select dname from dept where dname like 'abc%';

 

no rows selected

 

 

execution plan

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

   0      select statement optimizer=choose

   1    0   index (range scan) of 'dept_id1' (non-unique)

 

 

 

 

statistics

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

          0  recursive calls

          0  db block gets

          1  consistent gets

          0  physical reads

          0  redo size

        221  bytes sent via sql*net to client

        372  bytes received via sql*net from client

          1  sql*net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          0  rows processed

 

sql>

 

通常,为了均衡查询的效率和dml的效率,我们要仔细的分析应用,找出来出现频率相对较多、字段内容较少(比如varchar2(1000)就不适合建立索引,而varchar2(10)相对来说就适合建立索引)的列,合理的建立索引,比如有时候我们希望建立复合索引,有时候我们更希望建立单键索引。

事实上,oracle的索引还有很多话题,如监视索引的使用情况,oracle 9i 推出的skip scan等等,在下一个讨论中,我们会对这些话题详细阐述。

 

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表