当前位置:网站首页>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).
边栏推荐
- Can MySQL CDC take out the op field
- 在中金证券上做基金定投安全吗?
- 云原生到底是什么?它会是未来发展的趋势吗?
- Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
- Who's still buying three squirrels
- Japanese professor sues Intel FPGA and SOC products for infringing a design patent
- Configure load balancing
- Floyd repeat
- SSH服务器拒绝密码,再试一次;PermitRootLogin yes无效问题
猜你喜欢

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

7-Zip 遭抵制?呼吁者定下“三宗罪”:伪开源、不安全、作者来自俄罗斯!

bash: ln: command not found

CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)

Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)

睡了二哥。。。

Live broadcast management project

106. 从中序与后序遍历序列构造二叉树

What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it

Who has the vision to cross the cycle?
随机推荐
How do clients request databases?
Japanese professor sues Intel FPGA and SOC products for infringing a design patent
SQL statement modify field type "suggestions collection"
Comparison between Oracle JDK and openjdk
睡了二哥。。。
问下群里的各位,有使用flink oracle cdc的logminer方案,在生产上稳定运行的实际
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
TC8:UDP_USER_INTERFACE_01-08
关于#数据库#的问题:GBase 8s中如何避免死锁
678. 有效的括号字符串
442. 数组中重复的数据
Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
Does anyone know why? The table structure is the source table MySQL CDC that has just been directly copied
High precision factorial
“中移链”国密引擎在BSN正式上线
数据中台咋就从“小甜甜”变成了“牛夫人”?
Ubuntu system installation and MySQL configuration
Hardware midrange project
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability