当前位置:网站首页>[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 .
边栏推荐
- 基于FPGA的VGA协议实现
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- Why my order by create_ Time ASC becomes order by ASC
- leetcode:5270. Minimum path cost in Grid [simple level DP]
- Dacom G150双模耳机,为爱发声,呵护孩子听力健康成长
- In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
- 超级重磅!Apache Hudi多模索引对查询优化高达30倍
- Mysql database (28): Variables
- How to modify the authorization of sina Weibo for other applications
- Voir les pages du site
猜你喜欢

Go package import mode member visibility

CVPR 2022 oral Dalian Institute of technology proposed SCI: a fast and powerful low light image enhancement method

OpenGL shadow implementation (hard shadow)

【历史上的今天】6 月 12 日:美国进入数字化电视时代;Mozilla 的最初开发者出生;3Com 和美国机器人公司合并

一种灵活注入 Istio Sidecar 的方案探索

Embedded development: 6 necessary skills for firmware engineers

How to modify the authorization of sina Weibo for other applications

Uniapp uses the Ali Icon

liunx部署Seata(Nacos版)

In 2021, the global revenue of chlorinated polyvinyl chloride (CPVC) was about $1809.9 million, and it is expected to reach $3691.5 million in 2028
随机推荐
Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
leetcode:6095. Strong password verifier II [simple simulation + direct false]
Embedded development: 6 necessary skills for firmware engineers
leetcode:98. 统计得分小于 K 的子数组数目【双指针 + 计算子集个数 + 去重】
Kali2022 how to install w3af
Research Report on the overall scale, major manufacturers, major regions, products and applications of Electric Screwdrivers in the global market in 2022
[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics
In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
观察网站的页面
基于FPGA的VGA协议实现
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
每日一博 - 微服务权限一二事
OpenGL shadow implementation (hard shadow)
yoloe 目标检测使用笔记
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
SCI Writing - Methodology
leetcode:5259. 计算应缴税款总额【简单模拟 + 看看在哪个区间】
leetcode:6096. 咒语和药水的成功对数【排序 + 二分】
A journey of database full SQL analysis and audit system performance optimization