oracle中的exists 用法以及效…

  Ta,Tb兩張數據庫表關聯列爲 Ta.aid = Tb.bid,現在要取 Ta 中的數據,其中Ta.aid的值在b中也存在:
  SQLl實現如下:
  select * from Ta where exists(select 1 from Tb where Ta.aid = Tb.bid) 
  
   福利一枚:
   現在要取 Ta 中的數據,其中Ta.aid在Tb中不存在: 
  select * from  Ta where not exists(select 1 from Tb where Ta.aid = Tb.bid)


    有兩個簡單例子,以說明 “exists”和“in”的效率問題

    1) select * from Ta where exists(select 1 from Tb where Ta.aid = Tb.bid) ;

     Ta數據量小而Tb數據量非常大時,Ta<<Tb 時,1) 的查詢效率高。

    2) select * from Ta where Ta.aid in (select Tb.bid from Tb) ;

     Ta數據量非常大而Tb數據量小時,Ta>>Tb 時,2) 的查詢效率高。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章