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

1

2

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

1

2

子查询中,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

3

此语句查出了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 要快。

时间: 2024-08-12 08:53:43

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. NOT IN.NOT EXISTS.LEFT JOIN...IS NULL性能分析 我们首先创建测试表 USE TSQL2012 GO CREATE SCHEMA [compare] CREATE TABLE [compare].t_left ( id INT NOT NULL PRIMARY KE

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

博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源(  in与exists语句的效率问题):http://www.cnblogs.com/iceword/archive/2011/02/15/1955337.html (一) exists (sql 返回结果集为真) not exists (sql 不返回结果集为真) 如下: 表A ID NAME 1    A1 2

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 select 1 result2 from dual where 1 not in (2, 3, null); 5 6 7 select 1 result3 from dual where 1 in (2, 3, null, 1); 8 9 10 select 1 result4 from dual

sqlserver exists和in 与exists和not in

1.exists 和 in 1.1 正常情况下exists和in的效果是一样的,如图试验 即使子查询中包含null也没有关系,依然可以正常使用 1.2 in 和 exists效率比较 先看in 由图中可以100万的数据 用in的时间是14秒 用exist的时间也是14秒. 总结的规律是in和exists查询的效率相差不多 2.not in 和not exists 可以看出not in 如果子查询中有NUL将不会出现任何值. 所以再用not in时,最好在子查询中加上is not null 如图:

in和exists的区别以及exists和distinct去重的区别?

小编相信大家都知道in和exists的区别:1.运用情况不同sql中in适用于子查询得出的结果集记录较少,主查询中的表较大且又有索引的表,.sql中exist适用于外层的主查询记录较少,子查询中的表大,又有索引的时候. 2.驱动顺序不同IN是先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选.exists是以外层表为驱动表,先被访问. 3.底层原理不同in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 但是我昨天看到

if exists和if not exists关键字用法

转载自:http://blog.sina.com.cn/s/blog_b5fe6b270101ahc1.html 1.介绍 if not exists 即如果不存在,if exists 即如果存在 2.使用 a.判断数据库不存在时 if not exists(select * from sys.databases where name = 'database_name') b.判断表不存在时 if not exists (select * from sysobjects where id = o

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

前言 本节我们来分析LEFT JOIN和NOT EXISTS,简短的内容,深入的理解,Always to review the basics. LEFT JOIN...IS NULL和NOT EXISTS分析 之前我们已经分析过IN查询在处理空值时是基于三值逻辑,只要子查询中存在空值此时则没有任何数据返回,而LEFT JOIN和NOT EXISTS无论子查询中有无空值上处理都是一样的,当然比较重要的是利用LEFT JOIN...IS NULL来检查NULL.基于二者返回的结果集是一样的,下面我们

Oracle not in查不到应有的结果(NULL、IN、EXISTS详解)

问题: 语句1 : Select   *   from   table1 A  where  A.col1  not   in  (  select  col1  from  table2 B )  转载注明出处:http://x- spirit.iteye.com/.http: //www.blogjava.net/zhangwei217245/ 如果这样,本来应该有一条数据,结果没有. 如果我改写成这样: 语句2 : select   *   from table1 A where   no

SQL中EXISTS的用法

比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID FROM Orders o WHERE o.CustomerID=c.CustomerID) 这里面的EXISTS是如何运作呢?子查询返回的是OrderId字段,可是外面的查询要找的是CustomerID和CompanyName字段,这两个字段肯定不在OrderID里面啊,这是如何匹配的呢? EXIS