(轉)ORACLE 中IN和EXISTS的區別

xz43發表於2010-12-07
EXISTS的執行流程     
select * from t1 where exists ( select null from t2 where y = x )
可以理解為:
  for x in ( select * from t1 )
  loop
      if ( exists ( select null from t2 where y = x.x )
      then
        OUTPUT THE RECORD
      end if
  end loop
對於in 和 exists的效能區別:
  如果子查詢得出的結果集記錄較少,主查詢中的表較大且又有索引時應該用in,反之如果外層的主查詢記錄較少,子查詢中的表大,又有索引時使用exists。
  其實我們區分in和exists主要是造成了驅動順序的改變(這是效能變化的關鍵),如果是exists,那麼以外層表為驅動表,先被訪問,如果是IN,那麼先執行子查詢,所以我們會以驅動表的快速返回為目標,那麼就會考慮到索引及結果集的關係了
                         
另外IN時不對NULL進行處理
如:
select 1 from dual where null  in (0,1,2,null)

為空

2.NOT IN 與NOT EXISTS:     
NOT EXISTS的執行流程
select .....
  from rollup R
where not exists ( select 'Found' from title T
                            where R.source_id = T.Title_ID);
可以理解為:
for x in ( select * from rollup )
      loop
          if ( not exists ( that query ) ) then
                OUTPUT
          end if;
      end;

注意:NOT EXISTS 與 NOT IN 不能完全互相替換,看具體的需求。如果選擇的列可以為空,則不能被替換。

例如下面語句,看他們的區別:
select x,y from t;
x              y
------        ------
1              3
3        1
1        2
1        1
3        1
5
select * from t where  x not in (select y from t t2  )
no rows
     
select * from t where  not exists (select null from t t2
                                                  where t2.y=t.x )
x      y
------  ------
5      NULL
所以要具體需求來決定

對於not in 和 not exists的效能區別:
  not in 只有當子查詢中,select 關鍵字後的欄位有not null約束或者有這種暗示時用not in,另外如果主查詢中表大,子查詢中的表小但是記錄多,則應當使用not in,並使用anti hash join.
  如果主查詢表中記錄少,子查詢表中記錄多,並有索引,可以使用not exists,另外not in最好也可以用/*+ HASH_AJ */或者外連線+is null
NOT IN 在基於成本的應用中較好

比如:
select .....
from rollup R
where not exists ( select 'Found' from title T
                          where R.source_id = T.Title_ID);

改成(佳)

select ......
from title T, rollup R
where R.source_id = T.Title_id(+)
    and T.Title_id is null;
                               
或者(佳)
sql> select /*+ HASH_AJ */ ...
        from rollup R
        where ource_id NOT IN ( select ource_id
                                              from title T
                                              where ource_id IS NOT NULL )

注意:上面只是從理論上提出了一些建議,最好的原則是大家在上面的基礎上,能夠使用執行計劃來分析,得出最佳的語句的寫法
希望大家提出異議

以上的原則,我經過測試,發現有點問題,透過以下指令碼,外層表只有1條記錄滿足條件,而字查詢有很多記錄的情況下,In比Exists快很多很多了。

select st.id_card, st.is_approve, st.status
  from t_student st
 where st.status = 1
   and st.id in (select tr.student_id
                   from t_transfer tr
                  where tr.source_status = 3
                    and tr.flow_status = 0)
   and st.id_card = '511324199209270853';

select st.id_card, st.is_approve, st.status
  from t_student st
 where st.status = 1
   and exists (select tr.id
          from t_transfer tr
         where tr.source_status = 3
           and tr.student_id = st.id
           and tr.flow_status = 0)
   and st.id_card = '511324199209270853';

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9399028/viewspace-681059/,如需轉載,請註明出處,否則將追究法律責任。

相關文章