当前位置:网站首页>[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 .
边栏推荐
- The 14th five year development plan and investment prospect analysis report of China's oil and gas pipeline engineering construction 2022-2027
- chrome浏览器解决跨域问题
- A journey of database full SQL analysis and audit system performance optimization
- Why my order by create_ Time ASC becomes order by ASC
- 基于FPGA的VGA协议实现
- Istio 1.14 发布
- Voir les pages du site
- 觀察網站的頁面
- 论大型政策性银行贷后,如何数字化转型 ?-亿信华辰
- leetcode:5270. 网格中的最小路径代价【简单层次dp】
猜你喜欢

Chrome browser solves cross domain problems

How to download proxystrike in China

leetcode:5289. 公平分发饼干【看数据范围 + dfs剪枝】

OpenGL shadow implementation (hard shadow)

【0008】无序列表
![leetcode:98. Count the number of subarrays whose score is less than k [double pointers + number of calculated subsets + de duplication]](/img/97/67d74ae93be2cd4c0153a764cbe4c1.png)
leetcode:98. Count the number of subarrays whose score is less than k [double pointers + number of calculated subsets + de duplication]

Uniapp uses the Ali Icon

攻防世界(web篇)---supersqli

leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】

Research Report on the overall scale, major manufacturers, major regions, products and applications of Electric Screwdrivers in the global market in 2022
随机推荐
即时配送的订单分配策略:从建模和优化-笔记
leetcode:98. 统计得分小于 K 的子数组数目【双指针 + 计算子集个数 + 去重】
Super heavy! Apache Hudi multimode index optimizes queries up to 30 times
从应无所住说起
wireshark基本使用命令
Leetcode 494. 目标和
Order allocation strategy for instant delivery: from modeling and Optimization - Notes
笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
Double non grind one, three side byte, cool. Next time
Transactions in redis
no available service ‘null‘ found, please make sure registry config correct
MySQL数据库实验一 数据定义
Cookie & session & kaptcha verification code
Embedded development: 6 necessary skills for firmware engineers
Istio 1.14 release
Lua record
【图像去噪】基于正则化实现图像去噪附matlab代码
Go package import mode member visibility
Use of nexttick function
How to break the black screen after cleaning the dust and applying silicone grease on the laptop?