The most complete execution order of SQL Select statement

 

(From: http://blog.csdn.net/zengcong2013/article/details/42424915)

 

The complete execution sequence of the SQL Select statement: 

1. The from clause assembles data from different data sources; 

2. The where clause filters the rows based on the specified conditions; 

3. The group by clause divides the data into multiple groups; 

4. Use aggregate functions to calculate; 

5. Use the having clause to filter groups; 

6. Calculate all expressions; 

7. The field of select;

8. Use order by to sort the result set.

 

The most obvious feature of the SQL language that differentiates it from other programming languages ​​is the order in which the code is processed. In most database languages, codes are processed in encoding order. But in an SQL statement, the first clause to be processed is FROM, not the first SELECT.

Step number of SQL query processing:

(1) FROM <left_table>

(3) <join_type> JOIN <right_table>

(2) ON <join_condition>

(4) WHERE <where_condition>

(5) GROUP BY <group_by_list>

(6) WITH {CUBE | ROLLUP}

(7) HAVING <having_condition>

(8) SELECT

(9) DISTINCT

(9) ORDER BY <order_by_list>

(10) <TOP_specification> <select_list>

 

  Each of the above steps produces a virtual table that is used as input for the next step. These virtual tables are not available to callers (client applications or external queries). Only the table generated in the last step will be given to the caller. If a clause is not specified in the query, the corresponding step will be skipped.

  Introduction to the logical query processing stage:

  1. FROM: Perform a Cartesian product (cross join) on the first two tables in the FROM clause to generate a virtual table VT1.

  2. ON: Apply the ON filter to VT1, only those that are true are inserted into TV2.

  3. OUTER (JOIN): If OUTER JOIN is specified (as opposed to CROSS JOIN or INNER JOIN), the rows that do not find a match in the reserved table will be added to VT2 as outer rows to generate TV3. If the FROM clause contains more than two tables, repeat steps 1 through 3 for the resulting table from the previous join and the next table until all table positions have been processed.

  4. WHERE: Apply the WHERE filter to TV3, and insert TV4 only if it is true.

  5. GROUP BY: Group the rows in TV4 according to the column list in the GROUP BY clause to generate TV5.

  6. CUTE|ROLLUP: Insert the supergroup into VT5 to generate VT6.

  7. HAVING: Apply the HAVING filter to VT6, and only the group that is true is inserted into VT7.

  8. SELECT: Process the SELECT list and generate VT8.

  9. DISTINCT: Remove duplicate lines from VT8, product VT9.

  10. ORDER BY: Generate a cursor (VC10) by placing the rows in VT9 in the order of the column list in the ORDER BY clause.

       11. TOP: Select the specified number or proportion of rows from the beginning of VC10, generate table TV11, and return it to the caller.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326120783&siteId=291194637