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的更多相关文章

  1. 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. ...

  2. magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists

    相信有更新magento或者,备份转移magento站点的时候可能会碰到类似这样的错误提示: Base table or view already exists: 1050 Table ... alr ...

  3. oracle中的exists 和not exists 用法 in与exists语句的效率问题

    博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源(  in与exi ...

  4. sql server if exists和 if not exists 的关键字用法

    if exists和if not exists关键字用法   1.介绍  if not exists 即如果不存在,if exists 即如果存在 2.使用  a.判断数据库不存在时  if not ...

  5. 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 ...

  6. not exists、left join/is null、not in 行为

    测试数据 20:25:52[test](;)> select * from t;+------+------+| id   | b    |+------+------+|    1 | NUL ...

  7. sqlserver exists和in 与exists和not in

    1.exists 和 in 1.1 正常情况下exists和in的效果是一样的,如图试验 即使子查询中包含null也没有关系,依然可以正常使用 1.2 in 和 exists效率比较 先看in 由图中 ...

  8. if exists和if not exists关键字用法

    在sql语名中,if not exists 即如果不存在,if exists 即如果存在. 下面学习下二者的用法. a,判断数据库不存在时 代码示例: if not exists(select * f ...

  9. SQL Server-聚焦LEFT JOIN...IS NULL AND NOT EXISTS性能分析(十七)

    前言 本节我们来分析LEFT JOIN和NOT EXISTS,简短的内容,深入的理解,Always to review the basics. LEFT JOIN...IS NULL和NOT EXIS ...

随机推荐

  1. Swift学习笔记十四

    Deinitialization 当类的实例对象即将要被释放时,会立即调用deinitializer,通过deinit关键字来定义deinitializer,和initializer一样,它也只存在于 ...

  2. 从零开始学android开发-adt-bundle-eclipse下的修改android app名称

    eclipse中,打开项目根目录中的AndoirManifest.xml文件,找到如下内容 <application android:allowBackup="true" a ...

  3. 关于android 图像格式问题

    这算是篇总结吧.6月份开始做的一个android上的ar项目结束了.我做的部分是二维码识别和图像识别的预处理.这个项目虽然很累,但是让我学到了很多东西,特别是严格的编码规则,和java代码的效率优化, ...

  4. 【剑指offer】递归循环两种方式反转链表

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25737023 本文分别用非递归和递归两种方式实现了链表的反转,在九度OJ上AC. 题目描写 ...

  5. 【转】SoapUI5.0创建WebService接口模拟服务端

    原文:http://blog.csdn.net/a19881029/article/details/26348627 使用SoapUI创建WebService接口模拟服务端需要接口描述文件 MathU ...

  6. 【JavaScript】关于delete

    delete 只能删除属性,不能删除变量 比如 var m = "haha"; delete m; //false m = "haha";----->wi ...

  7. stm32 IAP + APP ==>双剑合一

    (扩展-IAP主要用于产品出厂后应用程序的更新作用,上一篇博文详细的对IAP 升级程序做了详细的分析http://blog.csdn.net/yx_l128125/article/details/12 ...

  8. Spring源码解析之:Spring Security启动细节和工作模式--转载

    原文地址:http://blog.csdn.net/bluishglc/article/details/12709557 Spring-Security的启动加载细节   Spring-Securit ...

  9. 代码片段---判断一文件是不是字符设备如果是把它拷贝到 /dev目录下

    #!/bin/sh myfile=/home/liu 这个是文件的路径 fd = `ls -l myfile` 获取文件的所有属性 fp= ${fd::} if ["$fp" = ...

  10. linux安装问题

    java: cannot execute binary file问题 主要原因是 linux系统是32位的,jdk版本是64位的. 补充知识: 1.查看linux位数: #uname -a 如果有x8 ...