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. C++ 转型

    1.const_static的使用场景:接收一个const对象,但是想改变对象内容,使用const_static去除对象的常量性,然后可以修改对象. 2.dynamic_static的使用场景:从子类 ...

  2. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B. Guess the Permutation 水题

    B. Guess the Permutation 题目连接: http://www.codeforces.com/contest/618/problem/B Description Bob has a ...

  3. UVALive 4192 Close Enough Computations 水题

    Close Enough Computations 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge& ...

  4. Gym 100531H Problem H. Hiking in the Hills 二分

    Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...

  5. UVA 12897 Decoding Baby Boos 暴力

    Decoding Baby Boos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  6. Swift用UIBezierPath来画圆角矩形、自定义多路径图形

    最好的特点就是可以自定义路径,设置圆角和描边都很方便,以下为代码和效果,均在playground中实现 1.首先实现一个圆角矩形,并对此路径描边,为其绘制一个轮廓. 1 2 3 4 5 6 7 8 9 ...

  7. 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...

  8. 剑指 offer set 7 调整数组顺序使奇数位于偶数前面

    总结 1. 之前不确定这种题的最终解法, 现在明确了, 就是一次快排

  9. Oracle的trunc和dbms_random.value随机取n条数据

    今天在review项目代码的时候看到这样一个问题,有一张号码表,每次需要从这样表中随机取6个空闲的号码,也就是每次取出来的6个号码应该都会有所不同.然后我就看到了这样的SQL select   t.* ...

  10. Transact-SQL三值逻辑

    /*===========================<一>========================== 在SQL中逻辑表达式的值有三种: 1.TRUE 2.FALSE 3.U ...