zoukankan      html  css  js  c++  java
  • oracle中exists和in的比较

    exists 是Oracle sql中的一个函数。表示是否存在符合某种条件的记录。如 

    select * from A,B 
    where A.id=B.id 
    and exists (SELECT * 
    FROM A 
    WHERE A.type LIKE 'S%') 

    它和Oracle的另外一个函数IN很相似,你可以比较一下他们的用法,见下:

    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.

     以后要注意凡是这样的比较问题,一般答案都不是绝对的,要分情况

     

     in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。一直以来认为exists比in效率高的说法是不准确的。

     

     如果查询的两个表大小相当,那么用in和exists差别不大。

      如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:

     

     

    ---转载自http://blog.csdn.net/hakunamatata2008/article/details/4239831

  • 相关阅读:
    MVC(一)
    C# 泛型(二)
    C# 泛型(一)
    ASP.NET MVC Razor
    ASP.NET 服务端接收Multipart/form-data文件
    centos(网易163)软件源更换
    xshell中文乱码问题
    centos7修改主机名
    sqlalchemy python中的mysql数据库神器
    mysql 更新与查询(排序 分组 链接查询)
  • 原文地址:https://www.cnblogs.com/qqzy168/p/3308657.html
Copyright © 2011-2022 走看看