SQL:NOT IN与NOT EXISTS不等价

在对SQL语句进行性能优化时,经常用到一个技巧是将IN改写成EXISTS,这是等价改写,并没有什么问题。问题在于,将NOT IN改写成NOT EXISTS时,结果未必一样。

目录

一、举例验证

例如,有如下一张表 rr 。要求:选择4月2号的数据,并且其type1是4月1号没有的(从表看,就是4月2号C的那条)。

  • 使用NOT IN ,单纯按照这个条件去实现
sql 复制代码
select * from rr 
where create_date='2024-04-02'
 and type1 not in (
		select type1 from rr 
		where create_date='2024-04-01'
	)
;
  • 使用NOT EXISTS
sql 复制代码
select r1.* from rr as r1
where r1.create_date='2024-04-02'
 and not exists (
		select r2.type1 from rr as r2 
		where r2.create_date='2024-04-01' and r1.type1=r2.type1
	)
;

主要原因是4月1号的数据中,存在type1为NULL的。如果该type1不是NULL,使用NOT IN就可以正确找出来结果了。

其中的原理涉及三值逻辑

二、三值逻辑简述

以下的式子都会被判为unknown

1、 = NULL

2、> NULL

3、< NULL

4、<> NULL

NULL = NULL
unknown ,它是因关系数据库采用了NULL而被引入的"第三个真值"。

(这里还有一点需要注意:真值unknown 和作为NULL的一种UNKNOWN(未知)是不同的东西。前者是明确的布尔类型的真值,后者既不是值也不是变量。为了便于区分,前者采用粗体小写字母unknown,后者用普通的大写字母UNKNOWN表示。)

加上true和false,这三个真值之间有下面这样的优先级顺序。

  • AND 的情况:false > unknown > true
  • OR 的情况:true > unknown > false

下面看具体例子,连同unknown 一起理解下

三、附录:用到的SQL

(运行环境Mysql)

1、表 rr 的构建

sql 复制代码
-- 使用了with语句
with rr as (
select '2024-04-01' as create_date,'A' as type1,001 as code1
 union all select '2024-04-01' as create_date,'A' as type1,002 as code1
 union all select '2024-04-01' as create_date,'A' as type1,002 as code1
 union all select '2024-04-01' as create_date,'B' as type1,013 as code1
 union all select '2024-04-01' as create_date,null as type1,013 as code1
 union all select '2024-04-02' as create_date,'B' as type1,013 as code1
 union all select '2024-04-02' as create_date,'C' as type1,109 as code1
 union all select '2024-04-03' as create_date,'A' as type1,002 as code1
 union all select '2024-04-04' as create_date,'A' as type1,002 as code1
)

2、 unknown的理解

sql 复制代码
set @a:=2, @b:=5, @c:= NULL ;

select @a+@b as result1,
       case when (@b>@c) is true then 'true!'
			      when (@b>@c) is false then 'false!'
			      else 'unknown'	
			 end as result2, -- 与NULL比较		 
       case when (@a<@b and @b>@c) is true then 'true!'
			      when (@a<@b and @b>@c) is false then 'false!'
			      else 'unknown'	
			 end as result3, -- and条件下 的优先级展示
			 case when (@a<@b or @b>@c) is true then 'true!'
			      when (@a<@b or @b>@c) is false then 'false!'
			      else 'unknown'	
			 end as result4, -- or条件下 的优先级展示
			 case when (not(@b<>@c)) is true then 'true!'
			      when (not(@b<>@c)) is false then 'false!'
			      else 'unknown'	
			 end as result5
相关推荐
RestCloud31 分钟前
ETL 不只是数据搬运工:如何实现智能转换与清洗?
数据库·api
lu9up33 分钟前
因表并行引发的血案【故障处理案例】
数据库·oracle·dba
问道飞鱼1 小时前
【大数据相关】ClickHouse命令行与SQL语法详解
大数据·sql·clickhouse
qq_356408661 小时前
es通过分片迁移迁移解决磁盘不均匀问题
java·数据库·elasticsearch
武子康2 小时前
Java-122 深入浅出 MySQL CAP理论详解与分布式事务实践:从2PC到3PC与XA模式
java·大数据·数据库·分布式·mysql·性能优化·系统架构
爱考证的小刘2 小时前
阿里云ACA认证[特殊字符]阿里云ACP认证
数据库·阿里云·云计算
Databend2 小时前
基于 Databend 的实时数据汇聚平台建设
数据库
玄尺_0073 小时前
bug:uniCloud报Business Failed, 参数有误retry invoke error
数据库·bug
@CLoudbays_Martin113 小时前
CDN是否能有效检测并且同时防御Ddos 和 CC 攻击?
java·服务器·网络·数据库·git·数据库开发·时序数据库
爱喝水的鱼丶3 小时前
SAP-MM:SAP采购组织全面学习指南:从概念到实战配置图解
运维·开发语言·数据库·学习·sap·mm模块·采购组织