in/exists not in/not exists null
in/not in exists/not exists null的理解
两个测试表
create table tmp01 as
with tmp as (
select '1' as id from dual
union all
select '2' as id from dual
union all
select '3' as id from dual
union all
select null as id from dual
)
select * from tmp;
create table tmp02 as
with tmp as (
select '1' as id from dual
union all
select '2' as id from dual
union all
select null as id from dual
)
select * from tmp;
select * from tmp01 t where t.id in (select * from tmp02);
ID
in可以理解为 t.id = 1 or t.id = 2 or t.id = null
select * from tmp01 t where t.id not in (select * from tmp02);
ID
no rows
not in 可以理解为 t.id <> 1 and t.id <> 2 and t.id <> null
由于t.id <> null,为unkown,始终返回false,所以查不出值来。
解决:
select * from tmp01 t where t.id not in (select * from tmp02 where id is not null) or t.id is null;
---------------------------------------------------------------------------------------------------------------------------------
exists实际上用的也是等值判断
select * from tmp01 t where exists (select 'X' from tmp02 d where d.id = t.id);
ID
子查询中,d.id = t.id 判断出来的只有 1 ,2,null = null不可判断,null只能使用 is null,is not null.
select * from tmp01 t where not exists (select 'X' from tmp02 d where d.id = t.id);
ID
此语句查出了null和3,子查询中查出的是1,2,不在这个集合中的有null和3
再说一下 in/exists的效率问题
两个表中一个数据量小,一个数据量大,则子查询表大的用exists,表小的用in
表tmp01(小表),表tmp02(大表)
select * from tmp01 where id in (select id from tmp02)
效率低,用到了tmp01 表上id 列的索引
select * from tmp01 where exists(select id from tmp02 where id= tmp01.id)
效率高,用到了tmp02 表上id 列的索引。
select * from tmp02 where exists(select id from tmp01 where id= tmp02.cc)
效率高,使用tmp02 表上id 列的索引
select * from tmp02 where exists(select id from tmp01 where id= tmp02.cc)
效率低,用到了tmp01表上id列的索引
not in/not exists
not in内外表进行全表扫描,不使用索引
not extsts 的子查询用到表上的索引。无论表大小,用not exists 都比not in 要快。
in/exists not in/not exists null的更多相关文章
- SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)
前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...
- magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists
相信有更新magento或者,备份转移magento站点的时候可能会碰到类似这样的错误提示: Base table or view already exists: 1050 Table ... alr ...
- oracle中的exists 和not exists 用法 in与exists语句的效率问题
博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源( in与exi ...
- sql server if exists和 if not exists 的关键字用法
if exists和if not exists关键字用法 1.介绍 if not exists 即如果不存在,if exists 即如果存在 2.使用 a.判断数据库不存在时 if not ...
- oralce中exists not exists in not in对于NULL的处理
1. 先讨论 in 与 not in中存在NULL的情况, sql语句如下: 1 select 1 result1 from dual where 1 not in (2, 3); 2 3 4 s ...
- not exists、left join/is null、not in 行为
测试数据 20:25:52[test](;)> select * from t;+------+------+| id | b |+------+------+| 1 | NUL ...
- sqlserver exists和in 与exists和not in
1.exists 和 in 1.1 正常情况下exists和in的效果是一样的,如图试验 即使子查询中包含null也没有关系,依然可以正常使用 1.2 in 和 exists效率比较 先看in 由图中 ...
- if exists和if not exists关键字用法
在sql语名中,if not exists 即如果不存在,if exists 即如果存在. 下面学习下二者的用法. a,判断数据库不存在时 代码示例: if not exists(select * f ...
- SQL Server-聚焦LEFT JOIN...IS NULL AND NOT EXISTS性能分析(十七)
前言 本节我们来分析LEFT JOIN和NOT EXISTS,简短的内容,深入的理解,Always to review the basics. LEFT JOIN...IS NULL和NOT EXIS ...
随机推荐
- cocos2dx的图片载入
//data: 图片文件数据 dataLen: 文件长度 bool Image::initWithImageData(const unsigned char * data, ssize_t dataL ...
- j2ee项目后置类代码实现跳出iframe页面跳转
response.getWriter().write("<script>top.location.href='../admin/login';</script>&qu ...
- Vs2008几个快捷键
CTRL+M 收缩 格式化cs代码:Ctrl+k+f 格式化aspx代码:Ctrl+k+d 5. 怎样快速切换不同的窗口? Ctrl+Tab 7. 怎样快速添加代码段? 输入prop然后按两 ...
- epoll的使用
http://blog.csdn.net/ljx0305/article/details/4065058 epoll - I/O event notification facility 在linu ...
- 内核链表list.h
stddef.h #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) list.h #ifndef _LINUX ...
- 代码片段------find批量处理
应用案例: 当前目录下有若干目录,有多个目录下有一个名叫build.sh的脚本,但是没有可执行权限,现在要将所有的build.sh假如可执行权限. pengdl@debian:~/work/SDK_3 ...
- 解决CSDN的code功能,无法git clone多个项目的问题
几天前在使用CSDN的git功能的时候发现一个问题:我在CSDN上创建了两个项目,但是却只能git clone其中的一个. 原因: 在添加ssh公钥的时候,将主机上的ssh公钥在CSDN上填的地方不合 ...
- google gflags使用.
code.google.com 被墙的好开心... gflags很简单. 编译使用都很简单. (不像omaha这种丧心病狂的编译依赖). cmake 生成一下. 一路顺风顺水. 值得注意的是: 默认 ...
- SQL Server 中的事务和锁(三)-Range S-U,X-X 以及死锁
在上一篇中忘记了一个细节.Range T-K 到底代表了什么?Range T-K Lock 代表了在 SERIALIZABLE 隔离级别中,为了保护范围内的数据不被并发的事务影响而使用的一类锁模式(避 ...
- android stuido 在线安装svn插件,添加版本库无响应
问题:android stuido 中在线安装svn插件,添加版本库无响应. 原因: 由于android stuido 版本较高,在线安装1.6x 版本的svn,添加版本库一直没有响应,最后卡死.. ...