Order of execution of SQL statements

Source: Internet
Author: User

The MySQL statement is divided into 11 steps, as noted, the first execution is always the from operation, and finally the limit operation is performed. Each of these operations produces a virtual table that acts as a processing input, except that the virtual tables are transparent to the user, but only the last virtual table is returned as a result. If you do not specify a clause in the statement, the corresponding step is skipped.

1 let's take a concrete look at each phase of query processing2 Form : The Cartesian product is computed on the left table of the From and the table on the right. Generating a virtual table VT13On: The virtual table VT1 is on filter, only those that conform to <join-condition>rows are recorded in the virtual table VT2. 4Join: If a outer join is specified (such as a LEFT JOIN or right join), rows that are not matched in the reserved table are added to the virtual table VT2 as an outer row, resulting in a virtual table VT3, which contains more than two tables in the rug FROM clause. Then the result of the previous join connection is VT3 and the next table repeats the steps 1~3 These three steps until all the tables have been processed. 5Where: the Where condition is filtered on the virtual table VT3. Only in accordance with <where-condition>will be inserted into the virtual table VT4. 6 GROUP By: Groups the records in VT4 according to the columns in the GROUP BY clause, producing VT5.7CUBE |ROLLUP: A cube or ROLLUP operation is performed on the table VT5, resulting in a table VT6.8Having: the use of the virtual table VT6 have filter, only in line with will be inserted into the virtual table VT7. 9 Select : Performs a select operation, selects the specified column, and inserts into the virtual table VT8. Ten DISTINCT: Go to the record in the VT8. Generates virtual table VT9. OneORDER by: Record records in virtual table VT9 according to <order_by_list>a sort operation that produces a virtual table VT10. ALIMIT: Takes a record of the specified row, generates a virtual table VT11, and returns the result.

The Order of writing: Select ... from ... where ... a group by ... have ... order by ... Limit [Offset,]
(rows)
Order of execution: From ... where...group by ... have ..... "Select ... order by ... limit"



Source: http://www.cnblogs.com/rollenholt/p/3776923.html

Order of execution of SQL statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.