[MySQL] SELECT statement - syntax order

1. SELECT 
2. DISTINCT <select_list>
3. FROM <left_table>
4. <join_type> JOIN <right_table>
5. ON <join_condition>
6. WHERE <where_condition>
7. GROUP BY <group_by_list>
8. HAVING <having_condition>
9. ORDER BY <order_by_condition>
10. LIMIT <limit_number>

As these steps execute, each step produces a virtual table that is used as input for the next step. These virtual tables support internal operations to take effect and are invisible to the outside world (client programs and external queries). Can only be used outside the last step to generate the table.

1. FROM stage

  • Cartesian product

It is the stage when we select the table. At this stage, the database will generate a virtual table VT1-J1 through Cartesian product of multiple data in the table.

  • ON stage

In this stage, the VT1-J1 generated in the previous step will be screened according to the predicates that appear in our ON sentence, and inserted into our VT1-J2.

  • JOIN stage

Add rows that do not find a match in VT1-J2 through JOIN, and add them to VT1-J2 as external rows to generate VT1-J3. If the FROM clause contains more than two tables, repeat the three steps for the result table VT1-J3 generated by the previous connection and the next table until all tables are processed.

After the above steps are completed, the FROM stage is completed.

2. WHERE stage

The WHERE stage is to filter the rows in VT1 according to the conditions in <where_condition>, so that the rows that meet the conditions will be inserted into VT2. At this time, the data has not been grouped, so the filtering of statistics cannot appear in WHERE.

3. GROUP BY stage

The GROUP stage groups the rows in VT2 according to the specified list of column names to generate VT3. In the end each group has only one row. In the GROUP BY phase, the database considers two NULL values ​​to be equal, so it will group the NULL values ​​into the same group.

4. HAVING stage

This stage filters the groups of VT3 according to the predicates appearing in the HAVING clause, and inserts the qualified groups into VT4. COUNT(expr) will return the number of rows where expr is not NULL, and count(1), count(*) will return all numbers including NULL values.

5. SELECT stage

This stage is the process of projection, which processes the elements mentioned in the SELECT clause and generates VT5. This step is generally carried out in the following order:

  • Evaluates the expressions in the SELECT list, producing VT5-1.
  • If there is DISTINCT, delete the duplicate rows in VT5-1 and generate VT5-2.

6. ORDER BY stage

According to the listed list specified in the ORDER BY clause, sort the rows in VT5-2 to generate VT6. If no sort is specified, the data is not always sorted in primary key order. NULL is considered the minimum value.

7. LIMIT stage

Take out the records of the specified row, generate the virtual table VT7, and return it to the query user. The efficiency of LIMIT n, m is very low. Generally,  WHERE  id > ? limit 10 can be optimized by specifying the range in the WHERE condition.

Guess you like

Origin blog.csdn.net/weixin_43918614/article/details/123260567