四 索引與分頁(yè)--怎么樣SQL運(yùn)行的更快
- 正確的使用索引
Where條件落在索引上
不要在where的=前使用函數(shù),否則無(wú)法使用索引
Is Null可能無(wú)法使用索引
不正確的隱式轉(zhuǎn)換可能不能使用索引
如果能在索引獲得數(shù)據(jù),就不要回表
如果是復(fù)合索引,注意第2個(gè)字段以后,可能使用不到索引
- 正確的使用hint
如果有別名,一定要有別名
格式如/*+ index(t index_name) */
- 無(wú)需回表查詢的分頁(yè)寫(xiě)法
存在以下表T1(A,B,C,D) T1上有索引字段(B,C) .如果只是查B,C兩個(gè)字段則:
select *
from (select tt.b, tt.c, rownum as rn
from (select t.b, t.c from t1 t where c = 2 order by t.c) tt
where rownum < 3)
where rn > 1
- 需回表查詢的分頁(yè)寫(xiě)法
select /*+ ordered use_nl(t,t1) */
*
from (select rid from (
select rownum rn,rid from (
select rowid rid from t1
where c=2
order by c desc)
where rownum <= 50)
where rn >=1) t,
t1
where t.rid=t1.rowid;