lhy2012 发表于 2013-1-14 08:52:24

oracle 中 in 跟 exists 的用法

例如
Users 表
   account   name
   1   张三
      2   李四
phone 表
    accounttelphone
   1    135678
   2    132789
1: exists 的用法
   Select account, name
    from users
where exists (select account from phone where telephone like ‘135%’)
返回的结果是:
       Account   name
       1          张三
使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项
2: in的用法
Select account, name
from users
whereaccount in (select account from phone where telephone like ‘135%’)
   Account   name
    1          张三
Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。
两者之间的效率比较:
IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。
页: [1]
查看完整版本: oracle 中 in 跟 exists 的用法