IN和EXISTS
有時候會將一列和一系列值相比較。最簡單的辦法就是在where子句中使用子查詢。在where子句中可以使用兩種格式的子查詢。
第一種格式是使用IN操作符:
... where column in(select * from ... where ...);
第二種格式是使用EXIST操作符:
... where exists (select 'X' from ...where ...);
我相信絕大多數人會使用第一種格式,因為它比較容易編寫,而實際上第二種格式要遠比第一種格式的效率高。在Oracle中可以幾乎將所有的IN操作符子查詢改寫為使用EXISTS的子查詢。
第二種格式中,子查詢以‘select 'X'開始。運用EXISTS子句不管子查詢從表中抽取什么數據它只查看where子句。這樣優化器就不必遍歷整個表而僅根據索引就可完成工作(這里假定在where語句中使用的列存在索引)。相對于IN子句來說,EXISTS使用相連子查詢,構造起來要比IN子查詢困難一些。
通過使用EXIST,Oracle系統會首先檢查主查詢,然后運行子查詢直到它找到第一個匹配項,這就節省了時間。Oracle系統在執行IN子查詢時,首先執行子查詢,并將獲得的結果列表存放在在一個加了索引的臨時表中。在執行子查詢之前,系統先將主查詢掛起,待子查詢執行完畢,存放在臨時表中以后再執行主查詢。這也就是使用EXISTS比使用IN通常查詢速度快的原因。
同時應盡可能使用NOT EXISTS來代替NOT IN,盡管二者都使用了NOT(不能使用索引而降低速度),NOT EXISTS要比NOT IN查詢效率更高。
EXISTS檢查是否有結果,判斷是否有記錄,返回的是一個布爾型(TRUE/FALSE)。
IN是對結果值進行比較,判斷一個字段是否存在于幾個值的范圍中,所以 EXISTS 比 IN 快。
主要區別是:
exists主要用于片面的,有滿足一個條件的即可,
in主要用于具體的集合操作,有多少滿足條件.
exists是判斷是否存在這樣的記錄,
in是判斷某個字段是否在指定的某個范圍內。
exists快一些吧 。
in適合內外表都很大的情況,exists適合外表結果集很小的情況。
在ASKTOM的講解:
Well, the two are processed very very differently.
Select * from T1 where x in ( select y from T2 )
is typically processed as:
select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;
The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then
joined to the original table -- typically.
As opposed to
select * from t1 where exists ( select null from t2 where y = x )
That is processed more like:
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
It always results in a full scan of T1 whereas the first query can make use of
an index on T1(x).
So, when is where exists appropriate and in appropriate?
Lets say the result of the subquery
( select y from T2 )
is "huge" and takes a long time. But the table T1 is relatively small and
executing ( select null from t2 where y = x.x ) is very very fast (nice index on
t2(y)). Then the exists will be faster as the time to full scan T1 and do the
index probe into T2 could be less then the time to simply full scan T2 to build
the subquery we need to distinct on.
Lets say the result of the subquery is small -- then IN is typicaly more
appropriate.
If both the subquery and the outer table are huge -- either might work as well
as the other -- depends on the indexes and other factors.