在什么情况下使用exist和in
2014-11-12 18:35
288 查看
http://www.itpub.net/thread-406784-4-1.html
You Asked (Jump to Tom's latest followup)
Tom:
can you give me some example at which situation
IN is better than exist, and vice versa.
and we said...
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.
You Asked (Jump to Tom's latest followup)
Tom:
can you give me some example at which situation
IN is better than exist, and vice versa.
and we said...
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.
相关文章推荐
- 显存会在什么情况下被使用
- [转]Web service到底是什么?在什么情况下,你应该使用web service
- 静态类在什么情况下使用
- 【转】在什么情况下应该使用adLockOptimistic,什么情况下应该使用adLockPessimistic....
- 在什么情况下使用 c++ member initialization list
- php各种编码集详解和以及在什么情况下进行使用
- sql 调优之-在子查询中使用exist还是 in
- HTTP协议中POST、GET、HEAD的区别是什么?分别在什么情况下使用?(
- webservice什么情况下使用?
- 同步和异步有何异同,在什么情况下分别使用他们?
- 最小堆/哈希表/二叉树/平衡二叉树/红黑树的意义(什么情况下使用)
- [转载]什么情况下该使用变量延迟?
- [转载]什么情况下该使用变量延迟?
- HTTP协议中POST、GET、HEAD的区别是什么?分别在什么情况下使用?
- [导入]SQL语句在什么情况下使用全表扫描?
- [转]Web service到底是什么?在什么情况下,你应该使用web service
- HTTP协议中POST、GET、HEAD的区别是什么?分别在什么情况下使用?
- Web service到底是什么?在什么情况下你应该使用Web service
- 同步与异步有何异同?在什么情况下分别使用他们?
- HTTP协议中POST、GET、HEAD的区别是什么?分别在什么情况下使用?