当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Redis雪崩、穿透和击穿是什么?

优品购电商3.0微服务商城项目实战小结

confluence7.4.X升级实录

【数据中台】数据中台的OneID是个什么鬼,主数据它不香吗?

黑马畅购商城---2.分布式文件存储FastDFS

Old ou, a fox friend, has had a headache all day. The VFP format is always wrong when it is converted to JSON format. It is actually caused by disordered code

Architects reveal the difference between working in Alibaba, Tencent and meituan

刷入Magisk通用方法

SDN system method | 9 Access network

The first techo day Tencent technology open day in 2022 will be held online on June 28
随机推荐
VFP a picture processing library, simple and easy to use, free of charge, worth recommending
ROS 笔记(06)— 话题消息的定义和使用
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
Dark horse shopping mall ---1 Project introduction - environment construction
VFP serial port communication is difficult for 9527. Maomao just showed his skill and was defeated by kiss
设置图片的透明度从左到右渐变
Effective reading of literature
Explain factor analysis in simple terms, with case teaching (full)
What are redis avalanche, penetration and breakdown?
Use PHP script to view the opened extensions
R language uses ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval corresponding to the v
R语言使用构建有序多分类逻辑回归模型、epiDisplay包的ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)
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
如果你也想做自媒体,不妨听大周给您点建议
Kotlin基础
The first techo day Tencent technology open day in 2022 will be held online on June 28
Arm V7 continuous load / store
R语言使用scale函数对神经网络的输入数据进行最小最大缩放、把数据缩放到0到1之间、并划分数据集为训练集和测试集
Cesium draw point line surface
Cesium editing faces