当前位置:网站首页>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).
边栏推荐
- 7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
- 关于#SQL#的问题,如何解决?
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- 编写自己的who命令
- Common penetration tools -goby
- 线程基础知识
- High precision factorial
- 怎么理解JS Promise
- Eat a rich woman's melon...
- 【黑马早报】俞敏洪称从来不看新东方股价;恒驰5将于7月开启预售;奈雪虚拟股票或涉嫌非法集资;7月1日起冰墩墩停产...
猜你喜欢

Sleeping second brother...

Eat a rich woman's melon...

Hardware midrange project

C# 一行代码计算文件的MD5值 - CodePlus系列

Strange, why is the ArrayList initialization capacity size 10?

Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture

Packetdrill script analysis guide

IDEA运行报错Command line is too long. Shorten command line for...

历史上的今天:九十年代末的半导体大战;冯·诺依曼发表第一份草案;CBS 收购 CNET...

架构实战营 模块九:设计电商秒杀系统
随机推荐
Write your own who commands
button按钮清除边框
数据中台咋就从“小甜甜”变成了“牛夫人”?
怎么理解JS Promise
Change password of MySQL version 5.7 and 8.0
4hutool practice: dateutil format time [easy to understand]
How did the data center change from "Britney Spears" to "Mrs. cow"?
The "China Mobile Chain" state secret engine was officially launched on BSN
venv: venv 的目录结构
SQL SERVER2014删除数据库失败,报错偏移量0x0000...
Module 9: design e-commerce seckill system
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;
Hardware midrange project
项目必用的全局异常处理器,你学会了吗
Who has the vision to cross the cycle?
sql语句修改字段类型「建议收藏」
在中金证券上做基金定投安全吗?
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
bash: ln: command not found
Which securities company has a low, safe and reliable Commission for stock trading and account opening