not in和not exist的区别(转)
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的区别(转)的更多相关文章
- 再一次见证mssql中in 与exist的区别
见下面代码 /*+' select * from '+@strDBName +'.dbo.m_aic where nodeid not in(select nodeid from @tmpAIC) ' ...
- sql in 和 exist的区别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp41 select * from A where id in(select ...
- oracle in和exist的区别 not in 和not exist的区别
in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询.一般大家都认为exists比in语句的效率要高,这种说法其实是不准确的,这个是要区分环境的. ...
- sql 中 in 与 exist 的区别
可以 通过 where 条件 把 null的情况 筛选掉,已避免出现上述的情况. 1, exist 返回 true or false: in 返回 true unknow. not之后 not ...
- MDX中Filter 与Exist的区别
获得一个集合,这个一般用来筛选出一个自定义的set,比如在中国的餐厅 该set返回所有MSDNteam下并且在Fact Thread度量上有记录的products 用Exists实现 sele ...
- mysql中in和exist的区别
mysql中in和exists的区别 -- in写法select * from A where A.id in (select bid from B ) and A.name in (select ...
- in与exist , not in与not exist 的区别(转)
in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的 ...
- in与exist , not in与not exist 的区别
in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. ...
- 浅谈SQL Server数据库分页
数据库分页是老生常谈的问题了.如果使用ORM框架,再使用LINQ的话,一个Skip和Take就可以搞定.但是有时由于限制,需要使用存储过程来实现.在SQLServer中使用存储过程实现分页的已经有很多 ...
随机推荐
- Android源代码解析之(六)-->Log日志
转载请标明出处:一片枫叶的专栏 首先说点题外话,对于想学android framework源代码的同学,事实上能够在github中fork一份,详细地址:platform_frameworks_bas ...
- Hbase Basic
启动:start-hbase.sh 停止:stop-hbase.sh 进入shell:hbase shell 状态:status 创建表:create 'tableName', 'colFam1' 查 ...
- Linux相互排斥与同步应用(三):posix线程实现单个生产者和单个消费者模型
[版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu.文章仅供学习交流,请勿用于商业用途] 在第一节说到了 ...
- Bootstrap学习速查表(四) 栅格系统
Bootstrap框架的网格系统工作原理如下: 1.数据行(.row)必须包含在容器(.container)中,以便为其赋予合适的对齐方式和内距(padding).如: 2.在行(.row)中可以添加 ...
- Oracle更新时间字段
update field set BEGINDATE=to_date('2015-05-03 10:30:20','yyyy-mm-dd hh24:mi:ss') where NOO='01'
- Spark源码分析之八:Task运行(二)
在<Spark源码分析之七:Task运行(一)>一文中,我们详细叙述了Task运行的整体流程,最终Task被传输到Executor上,启动一个对应的TaskRunner线程,并且在线程池中 ...
- iOS打包(ipa包)
1.打开XCode打开project文件.选择Product,再点击Archive. 2.鼠标右键点击Shoe In Finder 3.鼠标右键选择"显示包内容" 4.鼠标左键双击 ...
- erlang中遍历取出某个位置的最大值
例:有这么一个列表,A = [["abc","bds",3],["ssdss","dddx",2],["sfa ...
- mysql-mongdb-redis
千万级别:mysql 千万以及亿级别:mongdb
- 关于msbuild 编译.net 4.5新语法错误的解决方法
.net4.5以前msbuild 是在%windir%/Microsoft.NET/FrameworkXX/vXX目录下,如:C:\Windows\Microsoft.NET\Framework64\ ...