当前位置:网站首页>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’
 Insert picture description here
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’
 Insert picture description here

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 .

原网站

版权声明
本文为[lihongbao80]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200534500169.html

随机推荐