in和exists
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B(大表):select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引;select * from A where exists(select cc from B where cc=A.cc)
效率高,用到了B表上cc列的索引。
相反的2:select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引;select * from B where exists(select cc from A where cc=B.cc)
效率低,用到了A表上cc列的索引。 not in 和not exists 如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。所以无论那个表大,用not exists都比not in要快。 not in 逻辑上不完全等同于not exists,如果你误用了not in,小心你的程序存在致命的BUG: 请看下面的例子:
create table t1 (c1 number,c2 number);
create table t2 (c1 number,c2 number); insert into t1 values (,);
insert into t1 values (,);
insert into t2 values (,);
insert into t2 values (,null); select * from t1 where c2 not in (select c2 from t2);
no rows found
select * from t1 where not exists (select from t2 where t1.c2=t2.c2);
c1 c2 正如所看到的,not in 出现了不期望的结果集,存在逻辑错误。如果看一下上述两个select语句的执行计划,也会不同。后者使用了hash_aj。
因此,请尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询)。如果子查询中返回的任意一条记录含有空值,则查询将不返回任何记录,正如上面例子所示。
除非子查询字段有非空限制,这时可以使用not in ,并且也可以通过提示让它使用hasg_aj或merge_aj连接

not in和not exist的区别(转)的更多相关文章

  1. 再一次见证mssql中in 与exist的区别

    见下面代码 /*+' select * from '+@strDBName +'.dbo.m_aic where nodeid not in(select nodeid from @tmpAIC) ' ...

  2. sql in 和 exist的区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp41 select * from A where id in(select ...

  3. oracle in和exist的区别 not in 和not exist的区别

    in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询.一般大家都认为exists比in语句的效率要高,这种说法其实是不准确的,这个是要区分环境的. ...

  4. sql 中 in 与 exist 的区别

    可以 通过 where 条件 把 null的情况 筛选掉,已避免出现上述的情况. 1, exist 返回 true or  false:  in 返回  true  unknow. not之后 not ...

  5. MDX中Filter 与Exist的区别

        获得一个集合,这个一般用来筛选出一个自定义的set,比如在中国的餐厅 该set返回所有MSDNteam下并且在Fact Thread度量上有记录的products 用Exists实现 sele ...

  6. mysql中in和exist的区别

    mysql中in和exists的区别 -- in写法select * from A where A.id in (select bid from  B ) and A.name in (select ...

  7. in与exist , not in与not exist 的区别(转)

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

  8. in与exist , not in与not exist 的区别

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

  9. 浅谈SQL Server数据库分页

    数据库分页是老生常谈的问题了.如果使用ORM框架,再使用LINQ的话,一个Skip和Take就可以搞定.但是有时由于限制,需要使用存储过程来实现.在SQLServer中使用存储过程实现分页的已经有很多 ...

随机推荐

  1. hdu2141Can you find it?

     给你四个集合.要你从这四个集合中 各取出一个数出来,推断,取出的前三个数的和 是否等于第四个数. 数据比較大.我的做法是将 前两个集合全部数全部和的情况取出来, 然后二分查找第四个集合和第三集合 ...

  2. Cloudera

    官方文档: http://www.cloudera.com/content/cloudera/en/documentation/core/latest/ 博客教程 http://www.wangyon ...

  3. Chrome禁用NPAPI插件(包含 Silverlight、Java 和 Unity)

    过去,很多插件都是使用一种称为NPAPI 的旧系统开发的. 现在,仅仅有少量站点在使用NPAPI 插件,由于这些插件有时会给站点带来安全风险. 为了让用户获得更安全.更高速且更稳定的 Chrome 浏 ...

  4. php程序的三大流程控制

    php程序的三大流程控制 ①  顺序控制(从上到下.从左到右) ②分支控制 if(条件表达式){ //n多语句 }else if (条件表达式){ //n 多语句 }else if(条件表示式){ / ...

  5. HBase 系统架构及数据结构

    一.基本概念     2.1 Row Key (行键)     2.2 Column Family(列族)     2.3 Column Qualifier (列限定符)     2.4 Column ...

  6. 【PyCharm编辑器】之引用selenium包提示错误:Unresolved reference 'selenium' less... (Ctrl+F1)

    一.现象还原: 当新建.py文件时,需要引用selenium中的方法时,报错,提示红波浪线: Unresolved reference 'selenium' less... (Ctrl+F1) Thi ...

  7. go的timer定时器实现

    示例如下: package main import ( "fmt" "time" ) func testTimer1() { go func() { fmt.P ...

  8. IntelliJ IDEA生成 Serializable 序列化 UID 的快捷键

    首先创建一个类如Movie,让该类实现Serializable序列化接口. 然后我们需要依次按照以下的方法找到 Settings 之后我们需要以下几个操作,并找到 Serializable class ...

  9. mac下spark单机环境配置笔记

    1.安装scala 从http://www.scala-lang.org下载scala-2.11.7.tgz并解压缩 将解压缩的文件夹用mv指令移动到/usr/local/share mv [scal ...

  10. java 邮件(2)

    /**  * 方法描述:发送带附件的邮件  *   * @throws UnsupportedEncodingException  */  public static boolean sendEmai ...