Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the priority order of statement execution in sql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shares with you about the priority order in which statements are executed in sql. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The order in which SQL statements are executed

The statement of MySQL is divided into 11 steps, as noted in the following figure, the first is always the FROM operation, and the last is the LIMIT operation. Each of these operations produces a virtual table, which serves as an input for processing, except that these virtual tables are transparent to the user, but only the last virtual table is returned as a result. If a clause is not specified in the statement, the corresponding step is skipped.

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

FORM: calculates the Cartesian product of the left table and the right table of FROM. Generate virtual table VT1

ON: perform ON filtering on the virtual table VT1, and only those rows that match will be recorded in the virtual table VT2.

JOIN: if OUTER JOIN (such as left join, right join) is specified, 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 there are more than two tables in the rug from clause, the three steps of step 1 and 3 will be repeated for the result VT3 generated by the previous join join and the next table until all the tables have been processed.

WHERE: perform WHERE conditional filtering on the virtual table VT3. Only matching records are inserted into the virtual table VT4.

GROUP BY: according to the columns in the group by clause, the records in VT4 are grouped to generate VT5.

CUBE | ROLLUP: perform cube or rollup operations on table VT5 to generate table VT6.

HAVING: apply having filtering to the virtual table VT6, and only matching records will be inserted into the virtual table VT7.

SELECT: perform a select operation, select the specified column, and insert it into the virtual table VT8.

DISTINCT: de-duplicates the records in VT8. Generate virtual table VT9.

ORDER BY: the records in the virtual table VT9 are sorted according to the operation to generate the virtual table VT10.

LIMIT: fetch the record of the specified row, generate the virtual table VT11, and return the result.

The order of writing: select. From... Where.... Group by... Having... Order by.. Limit [offset,]

(rows)

Execution order: from... Where...group by... Having.... Select... Order by... Limit

Thank you for reading! This is the end of the article on "what is the priority order of sentence execution in sql". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report