Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3901786
  • 博文数量: 534
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4800
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(534)

文章存档

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(253)

2006年(73)

分类: Oracle

2011-05-20 22:22:28

关键字: 在oracle sql中取数据时in 和 exists 的区别? 在Oracle SQL中取数据时有时要用到in 和 exists 那么他们有什么区别呢?

1 性能上的比较
比如Select * from T1 where x in ( select y from T2 )
执行的过程相当于:
select *
  from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;

相对的

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
表 T1 不可避免的要被完全扫描一遍

分别适用在什么情况?
以子查询 ( select y from T2 )为考虑方向
如果子查询的结果集很大需要消耗很多时间,但是T1比较小执行( select null from t2 where y = x.x )非常快,那么exists就比较适合用在这里
相对应得子查询的结果集比较小的时候就应该使用in.
阅读(921) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~