当前位置:网站首页>SQL optimization - in and not in, exist
SQL optimization - in and not in, exist
2022-07-01 10:15:00 【zhangkaixuan456】
WHY?
IN and NOT IN Is a more commonly used keyword , Why try to avoid it ?
1、 Low efficiency
There's a situation in the project :
t1 surface and t2 surface All are 150w Data ,600M The appearance of , It's not big .
But such a query ↓
select * from t1 where phone not in (select phone from t2)
Just fooled me ... Ten minutes , I checked phone Index both tables , Field types are the same . original not in Can't hit the index ....
Change to NOT EXISTS Query after 20s , The efficiency is really poor, much better .
select * from t1
where not EXISTS (select phone from t2 where t1.phone =t2.phone)
2、 Be prone to problems , Or the query result is incorrect ( No more serious shortcomings )
With IN For example . Create two tables :test1 and test2
create table test1 (id1 int)
create table test2 (id2 int)
insert into test1 (id1) values (1),(2),(3)
insert into test2 (id2) values (1),(2)
I want to check , stay test2 Existing in test1 Medium id . Use IN The general way of writing is :
select id1 from test1
where id1 in (select id2 from test2)
The result is :

OK Wood problems !
But if I slip my hand for a moment , It has been written. :
select id1 from test1
where id1 in (select id1 from test2)
Don't be careful id2 It's written in id1 了 , What will happen ?
The result is :

EXCUSE ME! Why don't you make a mistake ?
Search separately select id1 from test2 Yes, it will definitely report an error : news 207, Level 16, state 1, The first 11 That's ok Name 'id1' Invalid .
However, it uses IN The sub query of is so perfunctory , Find out directly 1 2 3
This is just an error prone situation , If you don't write it wrong, there's nothing wrong , So let's see NOT IN Find out the wrong result directly :
to test2 Insert a null value :
insert into test2 (id2) values (NULL)
I want to check , stay test2 There is no such thing as test1 Medium id .
select id1 from test1
where id1 not in (select id2 from test2)
The result is :

blank ! Obviously, this result is not what we want . We want to 3. Why is this so ?
as a result of :NULL Not equal to any non empty value ! If id2 Only 1 and 2, that 3<>1 And 3<>2 therefore 3 Output , however id2 Contains a null value , that 3 Also is not equal to NULL So it won't output .
Digress : It's best not to allow null values when creating a table , Otherwise, there will be many problems .
be based on Spring Boot + MyBatis Plus + Vue & Element Implementation of the background management system + User applet , Support RBAC Dynamic permissions 、 multi-tenancy 、 Data access 、 workflow 、 Three party login 、 payment 、 SMS 、 Shopping malls and other functions .
Project address :https://github.com/YunaiV/ruoyi-vue-pro
HOW?
1、 use EXISTS or NOT EXISTS Instead of
select * from test1
where EXISTS (select * from test2 where id2 = id1 )
select * FROM test1
where NOT EXISTS (select * from test2 where id2 = id1 )
2、 use JOIN Instead of
select id1 from test1
INNER JOIN test2 ON id2 = id1
select id1 from test1
LEFT JOIN test2 ON id2 = id1
where id2 IS NULL
There's no problem !
PS: Then we can't live or die IN and NOT IN Why? ? did not , A great God once said , If it is a definite and finite set , have access to . Such as IN (0,1,2).
边栏推荐
- PHP code audit and File Inclusion Vulnerability
- 这样理解mmap,挺有意思!
- About widthstep of images in opencv
- CSDN一站式云服务开放内测,诚邀新老用户来抢鲜
- C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
- Floyd repeat
- 哪个券商公司炒股开户佣金低又安全又可靠
- What is cloud primordial? Will it be the trend of future development?
- 超标量处理器设计 姚永斌 第4章 分支预测 --4.1 小节摘录
- SQL server2014 failed to delete the database, with an error offset of 0x0000
猜你喜欢

leetcode:111. 二叉树的最小深度

SQL SERVER2014删除数据库失败,报错偏移量0x0000...

Introduction to expressions and operators in C language

SQL server2014 failed to delete the database, with an error offset of 0x0000

venv: venv 的目录结构

CCNP Part XII BGP (IV)

TC8:UDP_USER_INTERFACE_01-08

CSDN一站式云服务开放内测,诚邀新老用户来抢鲜

Hardware midrange project

Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
随机推荐
About widthstep of images in opencv
7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
106. 从中序与后序遍历序列构造二叉树
Thread Basics
How did the data center change from "Britney Spears" to "Mrs. cow"?
Have you learned the necessary global exception handler for the project
I like two men...
scratch大鱼吃小鱼 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月
bash: ln: command not found
这样理解mmap,挺有意思!
mysql cdc能把能把op字段拿出来吗
【Laravel 】faker数据填充详解
零基础入门测试该学什么?最全整理,照着学就对了
关于OpenCV中图像的widthStep
In terms of use
4hutool实战:DateUtil-格式化时间[通俗易懂]
In the new database era, don't just learn Oracle and MySQL
哪个券商公司炒股开户佣金低又安全又可靠
Hardware midrange project
SQL 化是 ETL 增量生产的第一步,这样的架构的核心能力是什么?