当前位置:网站首页>[SQL] MySQL query statement execution sequence analysis
[SQL] MySQL query statement execution sequence analysis
2022-06-12 19:00:00 【Game programming】


-- Example : select distinct s.id from T t join S s on t.id=s.id where t.name="Yrion" group by t.mobile having count(*)>2 order by s.create_timelimit 5;1:from
The first step is to choose from The table following the key words , This is also sql The first step of execution : Indicates which table to execute from the database .
Example is given to illustrate : In this case, first find the table from the database T
2:join on
join Is the table to be associated ,on It's the connection condition . adopt from and join on Select the database table to execute T and S, Produce Cartesian product , Generate T and S The merged interim table Temp1.on: Determine the binding relationship of the table , adopt on Create a temporary middle table Temp2.
Example is given to illustrate : Find the watch S, Generate temporary middle table Temp1, Then find the watch T Of id and S Of id The same parts make up the table Temp2,Temp2 It contains T and S id All equal data
3:where
where Means screening , according to where The following conditions are filtered , According to the value of the specified field ( If there is and The connector will be federated ) From temporary middle table Temp2 To filter the required data .
If no data is found at this stage , Will return to the client directly , Not going down . Otherwise, this process will generate a temporary intermediate table Temp3.
Be careful : stay where Aggregate functions and column aliases cannot be used in , Aggregate functions mainly include (min\max\count\sum Such as function )
Example is given to illustrate : stay temp2 Found... In temporary table set T Tabular name="Yrion" The data of , After finding the data, it will be a temporary middle table Temp3,temp3 It contains name As a "Yrion" All table data for
4:group by
group by It's grouping , Yes where Condition filtered temporary table Temp3 Group by fixed fields , Create a temporary middle table Temp4, This process is just a change in the order of the data , And the amount of data doesn't change , The data in the table exists as a group .
Example is given to illustrate : stay temp3 In the table data mobile Grouping , Find out mobile The same data , And put it together , produce temp4 A temporary table .
grammar :
group by Name 1, Name 2.... Name nBe careful group by Column aliases can be used after , No function can be used after that
5:Having
For the temporary middle watch Temp4 Aggregate , This can be count Wait for the count , And then the middle table Temp5, You can use... At this stage select Alias in
Example is given to illustrate : stay temp4 The number of items found in the temporary table is greater than 2 The data of , If it is less than 2 To be abandoned directly , Then generate a temporary middle table temp5
Be careful :having You can use the function after , as well as select Columns and column aliases , No one reported wrong.
6:select
Select the data to be queried from the grouped and aggregated tables , If * It will be resolved to all data , The middle table will be generated Temp6
Example is given to illustrate : At this stage is right temp5 In the temporary aggregation table S In the table id To produce Temp6, here temp6 It only includes s Tabular id Column data , also name=“Yrion”, adopt mobile The number of groups is greater than 2 The data of
7:Distinct
distinct De duplicate all data , If there is min、max Function will perform field function calculation and then perform de duplication , And then a temporary watch Temp7
Example is given to illustrate : This stage is right for temp5 The data in is de duplicated , engine API Will call the de duplication function to filter the data , In the end, just keep id The first piece of data , And then a temporary middle table temp7
Be careful :Distinct * and Distinct Name 1 The two are different
8:order by
Will be based on Temp7 To arrange in order or reverse order , Then insert the temporary middle table Temp8, This process is more resource consuming
Example is given to illustrate : This paragraph will bring all temp7 The data in the temporary table is based on the creation time (create_time) Sort , There will be no column or row loss in this process
Be careful :order by Then you can use the function , Query all fields or aliases in the table
9:limit
limit On the middle watch Temp8 paging , Create a temporary middle table Temp9, Return to the client .
Example is given to illustrate : stay temp7 Ordered data in , Then insert the first five into Temp9 In this temporary table , Finally back to the client
ps: In fact, this process is not absolutely so , middle mysql There will be some optimizations to achieve the best results , For example 6、7 Operation in 3 Wait after operation
author : Hui Jing
Game programming ️, A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- leetcode:5259. 计算应缴税款总额【简单模拟 + 看看在哪个区间】
- Implementation of VGA protocol based on FPGA
- Why my order by create_ Time ASC becomes order by ASC
- leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]
- 配送交付时间轻量级预估实践-笔记
- Daily blog - micro service permission 12 matters
- Redis中的事务
- uniapp使用阿里图标
- CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
- OpenGL shadow implementation (soft shadow)
猜你喜欢

kali通过iptables实现端口转发功能

I was badly hurt by the eight part essay...

被八股文害惨了。。。

A fruitful afternoon

io. seata. common. exception. FrameworkException: can not connect to services-server.

liunx部署Seata(Nacos版)

On how to make digital transformation after the loan of large policy banks- Yixinhuachen
![leetcode:6097. Match [set record + query one by one with the same length] after replacing characters](/img/03/d8286742bacf25b62c7dc3dcb0733e.png)
leetcode:6097. Match [set record + query one by one with the same length] after replacing characters

leetcode:5289. 公平分发饼干【看数据范围 + dfs剪枝】
![leetcode:5289. Distribute cookies fairly [see data range + DFS pruning]](/img/be/820bfb3aaf23a397e65f96693770f2.png)
leetcode:5289. Distribute cookies fairly [see data range + DFS pruning]
随机推荐
国内如何下载ProxyStrike
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Use of nexttick function
lua记录
leetcode:6096. 咒语和药水的成功对数【排序 + 二分】
Hash hash
Redis(三十二)-用Redis做分布式锁
【5GC】三种SSC(Session and Service Continuity)模式介绍
标准库template学习入门原创
What are the third-party software testing organizations in Shanghai that share knowledge about software validation testing?
Global and Chinese smart government industry market research and investment risk outlook report 2022-2028
Double non grind one, three side byte, cool. Next time
[image denoising] image denoising based on anisotropic filtering with matlab code
原生Servlet - 文件的Upload&Download
常用问题排查工具和分析神器,值得收藏
Redis中的事务
dumi 搭建文档型博客
Leetcode 494. 目标和
no available service ‘null‘ found, please make sure registry config correct
Dumi builds a document blog