以下優(yōu)化都是建立在表數(shù)據(jù)比較多的情況下,并且有建立索引(下面說(shuō)的都是圍繞索引來(lái)說(shuō)事)
- 避免多表查詢(xún)中連接條件不充分
如:表card上7000+行數(shù)據(jù),在card_no上有索引 , account表有20W+數(shù)據(jù) , 在account_no上有索引 , 看如下兩個(gè)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
- 避免在索引列上使用任何函數(shù)、運(yùn)算等操作
當(dāng)在索引列上使用函數(shù)、運(yùn)算操作時(shí),oracle是全表掃描而不是索引掃描
- 函數(shù)索引
是Oracle 8i +帶來(lái)的產(chǎn)物, 如果在name字段上建有索引 ,但是在使用時(shí)where條件經(jīng)常會(huì)substr(name,x,x),此是不會(huì)使用索引掃描而是使用全表掃描 , 此時(shí)就可以建立函數(shù)索引
select * from A where subtr(name,2,4) ;
-- 建立函數(shù)索引
create index 索引名 on 表名(subtr(name)) ;
SQL語(yǔ)句中,只要where子句使用了is null或is not null ,語(yǔ)句執(zhí)行時(shí)不會(huì)使用索引掃描
通配符(%)的like語(yǔ)句
如果某字段有索引 ,但是在where子句中使用like并且是%開(kāi)頭 ,執(zhí)行時(shí)則不會(huì)進(jìn)行索引掃描
-- 不會(huì)索引掃描
select * from A where name like '%a%' ;
-- 會(huì)索引掃描,因?yàn)?沒(méi)有在開(kāi)頭
select * from A where name like 'a%' ;
減少使用not <>或!= , 因?yàn)槭侨頀呙瓒粫?huì)走索引掃描(在有索引的情況)
使用union替換or (適用于索引列且數(shù)據(jù)量較大時(shí))
-- 低效 ( or不會(huì)走索引掃描)
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%' ;
- 使用 >= 替換 >
-- 下面兩個(gè)語(yǔ)句的執(zhí)行結(jié)果是一樣的
-- 高效
select name from A where id >= 3 ;
-- 低效
select name from A where id > 2 ;
-- 兩都區(qū)別:前者執(zhí)行時(shí)會(huì)直接跳轉(zhuǎn)到id等于3的記錄而后者會(huì)先定位到id等于2的記錄并向前掃描第一個(gè)id的大于2的記錄
盡量使用表連接代替exists、in
group by , order by 盡量寫(xiě)到SQL的最后