mysql statement execution order

 

MySQL statement execution order

The MySQL statement is divided into 11 steps, as marked in the following figure, the FROM operation is always executed first, and the LIMIT operation is executed last. Each of these operations produces a virtual table that is used as input to a process, but these virtual tables are transparent to the user, but only the last virtual table will be returned as a result. If a clause is not specified in the statement, the corresponding step will be skipped.



 

Let's analyze each stage of query processing in detail.

  1. FORM : Calculates the Cartesian product of the table on the left and the table on the right of FROM. Generate virtual table VT1
  2. ON : ON filter the virtual table VT1, only those rows that meet the <join-condition> will be recorded in the virtual table VT2.
  3. JOIN : If OUTER JOIN (such as left join, right join) is specified, then the unmatched rows in the reserved table will be added to the virtual table VT2 as external rows, resulting in a virtual table VT3, if the from clause contains more than two If there is a table, then the three steps of steps 1 to 3 will be repeated for the result VT3 generated by the previous join connection and the next table, until all the tables are processed.
  4. WHERE : Perform WHERE condition filtering on virtual table VT3. Only records that meet the <where-condition> will be inserted into virtual table VT4.
  5. GROUP BY : According to the columns in the group by clause, group the records in VT4 to generate VT5.
  6. CUBE | ROLLUP : Cube or rollup table VT5 to generate table VT6.
  7. HAVING : Apply having filtering to virtual table VT6, and only records that meet the <having-condition> will be inserted into virtual table VT7.
  8. SELECT : Execute the select operation, select the specified column, and insert it into the virtual table VT8.
  9. DISTINCT : Deduplicates records in VT8. Generate virtual table VT9.
  10. ORDER BY : Sort the records in virtual table VT9 according to <order_by_list> to generate virtual table VT10.
  11. LIMIT : Take out the record of the specified row, generate the virtual table VT11, and return the result.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942231&siteId=291194637