当前位置:网站首页>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 .
边栏推荐
- [论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer
- R language uses GLM function to build Poisson logarithmic linear regression model, processes three-dimensional contingency table data to build saturation model, and poisgof function of epidisplay pack
- SDN系统方法 | 9. 接入网
- Black Horse Chang Shopping Mall - - - 3. Gestion des produits de base
- SQL server saves binary fields to disk file
- Mui scroll bar recovery
- Redis雪崩、穿透和击穿是什么?
- What is Flink? What can Flink do?
- 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
- 一套自动化无纸办公系统(OA+审批流)源码:带数据字典
猜你喜欢

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

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

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

一款好用的印章设计工具 --(可转为ofd文件)

Découvrir gaussdb (pour redis): une comparaison complète avec Codis

What is principal component analysis? Dimension reduction of classical case analysis variables

Oracle Spatial creating spatial tables

What is Flink? What can Flink do?

What are redis avalanche, penetration and breakdown?

confluence7.4.X升级实录
随机推荐
Web project development process
Dark horse shopping mall ---6 Brand, specification statistics, condition filtering, paging sorting, highlighting
Flutter common commands and problems
Why should Apple change objc_ Type declaration for msgsend
Cesium editing faces
Hook technology
Windows下MySQL的安装和删除
Redis雪崩、穿透和击穿是什么?
Black Horse Chang Shopping Mall - - - 3. Gestion des produits de base
Dynamic proxy
揭秘GaussDB(for Redis):全面对比Codis
15. Notes on the button style of WPF
ARM V7 ldr str 内存访问
Encapsulation of practical methods introduced by webrtc native M96 basic base module (MD5, Base64, time, random number)
Cesium building loading (with height)
The first techo day Tencent technology open day in 2022 will be held online on June 28
19、wpf之事件转命令实现MVVM架构
一款好用的印章设计工具 --(可转为ofd文件)
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Quarkus SaaS dynamic data source switching implementation, simple and perfect