当前位置:网站首页>Execution order of MySQL query statements join, on and where
Execution order of MySQL query statements join, on and where
2022-06-25 12:23:00 【lihongbao80】
MySQL Execution order of
One 、 A typical SELECT The complete execution sequence of statements
1)from Clause to assemble data from different data sources ;
2) Use on Conduct join Connected data filtering
3)where Clause filters record rows based on specified criteria ;
4)group by Clause to divide data into groups ;
5)cube, rollup
6) Using aggregate functions to calculate ;
7) Use having Clause filter group ;
8) Calculate all expressions ;
9) Calculation select Field of ;
10) Use distinct Data De duplication
11) Use order by Sort the result set .
12) choice TOPN The data of
Two 、from
If it is adopted relation from tableA, tableB , this 2 The first table will organize Cartesian product , Then proceed to the following where、group by Wait for the operation .
3、 ... and 、on
If you use left join, inner join perhaps outer full join When , Use on After condition filtering , It's going on join.
Look at the following 2 individual sql And result .2 The difference between them is only in on The following statement is in on and where The difference in location . It can be seen from this that we passed first on Filter by conditions , And then in join, Finally, we are going to where Condition screening .
If : It's first join, It's going on on Words , Will produce a Cartesian product , Then in the filter . In this way left join and Direct connection There is no difference . So it must be first on After condition filtering , It's going on join.
If : It's going on where after , stay on, It's going on join, below 2 individual sql The return result of should be the same . From this we can see ,where Is aimed at join Filtering of the set after .
Sum up : First perform on Condition screening , It's going on join, The last part where Screening
SELECT DISTINCT a.domain , b.domain
FROM mal_nxdomains_raw a
LEFT JOIN mal_nxdomains_detail b ON a.domain = b.domain AND b.date = ‘20160403’
WHERE a.date = ‘20160403’
SELECT DISTINCT a.domain , b.domain
FROM mal_nxdomains_raw a
LEFT JOIN mal_nxdomains_detail b ON a.domain = b.domain #and b.date = ‘20160403’
WHERE a.date = ‘20160403’
AND b.date = ‘20160403’
Four 、on Conditions and where Conditions
1、 Use location
on The condition position is join Back
where The condition is join And on After the completion
2、 Use object
on The use object of is the associated table
where The use object of can be the main table , It can also be an associated table
3、 Choose and use
Main table condition filter : Only in where Use... In the back .
Associated table , If you want to shrink join Range , It can be placed in on Back . If it is an association, then query , It can be placed in where Back .
If left join in ,where The condition has a relation to the associated table Of the associated field Non empty query , And use inner join After the effect of , It's going on where The effect of screening is the same . Can't play left join The role of .
5、 ... and 、join technological process
tableA join tableB, from A Take a piece of data from the table , To B Scan the table for matching . therefore A The number of rows determines the number of queries ,B The number of rows in the table determines the scanning range . for example A surface 100 strip ,B surface 200 surface , need 100 Secondary slave A Take a piece of data from the table to B In the table 200 Times of comparison . Relatively speaking, from A Table fetching data consumes more resources . So try your best tableA Select a smaller table . At the same time, shrink B The query range of the table .
But in practice , Because they return different data results , The index used is also different , Cause the condition to be placed in on and where Efficiency is not necessarily who is better . It should be determined according to the needs .
边栏推荐
- 黑马畅购商城---8.微服务网关Gateway和Jwt令牌
- Hook technology
- WebRTC Native M96 基础Base模块介绍之实用方法的封装(MD5、Base64、时间、随机数)
- VFP serial port communication is difficult for 9527. Maomao just showed his skill and was defeated by kiss
- Mpai data science platform random forest classification \ explanation of regression parameter adjustment
- Kotlin基础
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用summary函数获取模型汇总统计信息
- The network traceroute command is used to determine the path through which IP packets access the destination address.
- 2022 meisai topic C idea sharing + translation
- Dark horse shopping mall ---2 Distributed file storage fastdfs
猜你喜欢
![[论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer

为什么ping不通网站 但是却可以访问该网站?

The cloud native data lake has passed the evaluation and certification of the ICT Institute with its storage, computing, data management and other capabilities

黑馬暢購商城---3.商品管理

devsecops与devops的理解与建设

Uncover gaussdb (for redis): comprehensive comparison of CODIS

一款好用的印章设计工具 --(可转为ofd文件)
![[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer

flutter常用命令及问题

2022 meisai topic C idea sharing + translation
随机推荐
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Why should Apple change objc_ Type declaration for msgsend
揭秘GaussDB(for Redis):全面对比Codis
19、wpf之事件转命令实现MVVM架构
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the summary function to obtain the summary statistical information
The first techo day Tencent technology open day in 2022 will be held online on June 28
Is it safe to open an account and buy stocks on the Internet?
ArcGIS services query filter by time field
Understanding and construction of devsecops and Devops
为什么ping不通网站 但是却可以访问该网站?
Kotlin基础
属性分解 GAN 复现 实现可控人物图像合成
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用summary函数获取模型汇总统计信息
R language uses the scale function to scale the input data of neural network to the minimum and maximum, scale the data to between 0 and 1, and divide the data set into training set and test set
黑馬暢購商城---3.商品管理
A detour taken by a hardware engineer
什么是Flink?Flink能用来做什么?
R语言caTools包进行数据划分、scale函数进行数据缩放、e1071包的naiveBayes函数构建朴素贝叶斯模型
An easy-to-use seal design tool - (can be converted to ofd file)
15、wpf之button样式小记