exists和in使用區別
有兩個簡單例子,以說明 “exists”和“in”的效率問題 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1數據量小而T2數據量非常大時,T1<<T2 時,1) 的查詢效率高。 2) select * from T1 where T1.a in (select T2.a from T2) ; T1數據量非常大而T2數據量小時,T1>>T2 時,2) 的查詢效率高。
總結:關聯后邊的查詢
1.內表必須和外表連接
select * from emp_tax o where exists (select * from emp_tax i where i.empno = o.empno and i.empno < 0005);
2.exist適合外表數據少,內表數據大
exists 適合外表的結果集小的情況。因為exists是對外表作loop,每次loop再對內表進行查詢。 當 exists 中的 where 后面條件為真的時候則把前面select 的內容顯示出來(外表的select ). Oracle exists 和 in 的真正區別 in 是把外表和內表作hash join,而exists是對外表作loop,每次loop再對那表進行查詢。 這樣的話,in適合內外表都很大的情況,exists適合外表結果集很小的情況。