ITPub博客

首页 > 数据库 > Oracle > 不使用索引的查询

不使用索引的查询

原创 Oracle 作者:cxck 时间:2007-05-05 23:05:01 0 删除 编辑
尽管创建了索引,但是由于不适当的查询条件,往往会导致索引失效。[@more@]

SQL>CREATE TABLE tTest(
id1 char(10) NOT NULL,
id2 char(10) NOT NULL,
id3 char(10) NOT NULL,
name varchar2(100),
value number(8)
);
SQL>CREATE INDEX idx_tTest_1 ON tTest(
id1
);
SQL>CREATE INDEX idx_tTest_2 ON tTest(
name
);
SQL>CREATE INDEX idx_tTest_3 ON tTest(
value
);
SQL>CREATE INDEX idx_tTest_4 ON tTest (
id1,id2,id3
);

■ 索引列使用NOT比较时
 × select * from tTest where id1 <> '001';
 × select * from tTest where id1 not in ('001','002','003');

■ 索引列使用函数时,不使用索引
 × select * from tTest where substr(id1,1,1) = '1';

■ 索引列运算时,不使用索引
 ○ select * from tTest where value > 100;
 × select * from tTest where value - 50 > 100;

■ 索引列和NULL比较时,不使用索引
  索引中不存在NULL值,如果指定NULL值,则不使用索引
 × select * from tTest where name is null;
■ 索引列前方一致以外的模糊匹配(LIKE)
 ○ select * from tTest where id1 = '001%';
 × select * from tTest where id1 = '%001';
 × select * from tTest where id1 = '%00%';

■ ORDER BY语句如果不指定所有索引列,排序时不使用索引
  ORDER BY语句指定的排序列,如果包括所有索引列,并且索引列NOT NULL的时,使用索引排序。
○ select * from tTest order by id1,id2,id3;

■ 不从组合索引第一列开始作为查询条件时
  组合索引idx_tTest_4
 (1)id1
 (2)id2
 (3)id3

  上述复合索引列条件查询时,索引使用情况
 ○ select * from tTest where id1 = '001' and id2 = '001' and id3 = '001';
 ○ select * from tTest where id1 = '001' and id2 = '001';
 ○ select * from tTest where id1 = '001' and id3 = '001';
 ○ select * from tTest where id1 = '001';
 × select * from tTest where id2 = '001';
 × select * from tTest where id3 = '001';
 × select * from tTest where id2 = '001' and id1 = '001' and id3 = '001';

说明:

○ 使用索引 × 不使用索引

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/174388/viewspace-913702/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    38
  • 访问量
    74699