当前位置:网站首页>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 .
边栏推荐
- 20、wpf之MVVM命令绑定
- 为什么ping不通网站 但是却可以访问该网站?
- 揭秘GaussDB(for Redis):全面對比Codis
- Arm immediate
- Ten commandments of self-learning in machine learning
- The cloud native data lake has passed the evaluation and certification of the ICT Institute with its storage, computing, data management and other capabilities
- Implementing Domain Driven Design - using the ABP framework - Summary of a series of articles
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、epiDisplay包的poisgof函数对拟合的泊松回归模型进行拟合优度检验(检验模型效果)
- 19. Implementation of MVVM architecture based on WPF event to command
- 刷入Magisk通用方法
猜你喜欢
Why can't you Ping the website but you can access it?
Dark horse shopping mall ---3 Commodity management
Architects reveal the difference between working in Alibaba, Tencent and meituan
Rank sum ratio comprehensive evaluation method for common models in mathematical modeling
优品购电商3.0微服务商城项目实战小结
Time series analysis - how to use unit root test (ADF) correctly?
15、wpf之button样式小记
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
Continue to cut the picture after the ArcGIS Server is disconnected
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer
随机推荐
什么是Flink?Flink能用来做什么?
Black Horse Chang Shopping Mall - - - 3. Gestion des produits de base
devsecops与devops的理解与建设
Dark horse shopping mall ---8 Microservice gateway and JWT token
WebRTC Native M96 基础Base模块介绍之实用方法的封装(MD5、Base64、时间、随机数)
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语言使用scale函数对神经网络的输入数据进行最小最大缩放、把数据缩放到0到1之间、并划分数据集为训练集和测试集
Arm V7 continuous load / store
网络 | 衡量网络好坏的指标及测试方法
黑马畅购商城---3.商品管理
How do super rookies get started with data analysis?
Cesium draw point line surface
Image tagging to obtain the coordinates of the image in the ImageView
Controllable character image synthesis based on attribute decomposition and Gan reproduction
使用php脚本查看已开启的扩展
R语言dplyr包filter函数过滤dataframe数据中指定数据列的内容不是(不等于指定向量中的其中一个)指定列表中的数据行
An easy-to-use seal design tool - (can be converted to ofd file)
Dark horse shopping mall ---2 Distributed file storage fastdfs
VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large
Découvrir gaussdb (pour redis): une comparaison complète avec Codis