当前位置:网站首页>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).
边栏推荐
- The "China Mobile Chain" state secret engine was officially launched on BSN
- SQL statement modify field type "suggestions collection"
- Change password of MySQL version 5.7 and 8.0
- SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
- 直播管理项目
- 在通达信上买基金安全吗?
- High precision factorial
- “中移链”国密引擎在BSN正式上线
- 请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
- Daily mathematics serial 55: February 24
猜你喜欢

Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
![C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列](/img/d2/dad88f53701c7cd7638bd4983cbb4b.png)
C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列

Fried money, lost 10million.

零基础入门测试该学什么?最全整理,照着学就对了

建议收藏 | 在openGauss上遇到慢SQL该怎么办?

Have you learned the necessary global exception handler for the project

leetcode:111. Minimum depth of binary tree

Who has the vision to cross the cycle?

Kotlin 协程调度切换线程是时候解开真相了
随机推荐
渗透常用工具-Goby
Have you learned the necessary global exception handler for the project
Does anyone know why? The table structure is the source table MySQL CDC that has just been directly copied
4hutool practice: dateutil format time [easy to understand]
Strange, why is the ArrayList initialization capacity size 10?
It is interesting to understand MMAP in this way!
Hardware midrange project
PHP 字符串与二进制相互转换
Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
建议收藏 | 在openGauss上遇到慢SQL该怎么办?
“中移链”国密引擎在BSN正式上线
主流实时流处理计算框架Flink初体验
Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years
Eat a rich woman's melon...
直播管理项目
Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
哪个券商公司炒股开户佣金低又安全又可靠
推荐一款 JSON 可视化工具神器!
Centos 配置discuz 提示请检查 mysql 模块是否正确加载
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly