Oracle 优化原则

2016-08-05  本文已影响105人  else05
以下优化都是建立在表数据比较多的情况下,并且有建立索引(下面说的都是围绕索引来说事)

如:表card上7000+行数据,在card_no上有索引 , account表有20W+数据 , 在account_no上有索引 , 看如下两个sql:

-- 20秒
select sum(a.amount) from account a, card b where a.card_no = b.card_no
-- < 5秒
select sum(a.amount) from account a, card b where a.card_no = b.card_no and a.account_no = b.account_no

当在索引列上使用函数、运算操作时,oracle是全表扫描而不是索引扫描

是Oracle 8i +带来的产物, 如果在name字段上建有索引 ,但是在使用时where条件经常会substr(name,x,x),此是不会使用索引扫描而是使用全表扫描 , 此时就可以建立函数索引

select * from A where subtr(name,2,4) ;
-- 建立函数索引
create index 索引名 on 表名(subtr(name)) ;

如果某字段有索引 ,但是在where子句中使用like并且是%开头 ,执行时则不会进行索引扫描

-- 不会索引扫描
select * from A where name like '%a%' ;
-- 会索引扫描,因为%没有在开头
select * from A where name like 'a%' ;
-- 低效 ( or不会走索引扫描)
select name from A where age = 10 or name like 'a%';
-- 高效 (前提是age和name建立了索引)
select name from A where age = 10
union
select name from name like 'a%' ;
-- 下面两个语句的执行结果是一样的
-- 高效
select name from A where id >= 3 ;
-- 低效
select name from A where id > 2 ;
 -- 两都区别:前者执行时会直接跳转到id等于3的记录而后者会先定位到id等于2的记录并向前扫描第一个id的大于2的记录

注:如有错误的地方请大家指出,谢谢

上一篇 下一篇

猜你喜欢

热点阅读