A brief understanding of MySQL SELECT execution order

A brief understanding of MySQL SELECT execution order

The complete syntax of the SELECT statement is:

(7) SELECT 
(8) DISTINCT <select_list>
(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) HAVING <having_condition>
(9) ORDER BY <order_by_condition>
(10) LIMIT <limit_number>

Note: The sequence number before the syntax is the SELECT execution order

The MySQL SELECT execution order is divided into 10 steps. As marked above, the first operation to be executed is the FROM operation, and the last operation to be executed is the LIMIT operation. Each operation will generate a virtual table, which is used as the input for processing. These virtual tables are transparent to the user, but only the last virtual table will be returned as the result. If a clause is not specified in the statement, the corresponding step will be skipped.

Let's take a closer look at each stage of query processing.

SELECT a.customer_id, COUNT(b.order_id) as total_orders
 FROM table1 AS a
 LEFT JOIN table2 AS b
 ON a.customer_id = b.customer_id
 WHERE a.city = 'hangzhou'
 GROUP BY a.customer_id
 HAVING count(b.order_id) < 2
 ORDER BY total_orders DESC;

1. FORM: Calculate the Cartesian product of the table on the left and the table on the right of FROM to generate the virtual table VT1.

2. ON: Perform ON filtering on virtual table VT1, and only those rows that meet <join-condition> will be recorded in virtual table VT2.

3. JOIN: If OUTER JOIN is specified (such as left join, right join), the unmatched rows in the retained table are added to virtual table VT2 as external rows, generating virtual table VT3.

4. WHERE: Filter the virtual table VT3 based on the WHERE condition. Only records that meet <where-condition> will be inserted into virtual table VT4.

5. GROUP BY: Group the records in VT4 according to the columns in the group by clause to generate VT5.

6. HAVING: Apply having filtering to virtual table VT5, and only records that meet <having-condition> will be inserted into virtual table VT6.

7. SELECT: Execute the select operation, select the specified column, and insert it into the virtual table VT7.

8. DISTINCT: Remove duplicate records in VT7. Generate virtual table VT8.

9. ORDER BY: Sort the records in virtual table VT8 according to <order_by_list> to generate virtual table VT9.

10. LIMIT: Take out the records of the specified row, generate virtual table VT10, and return the result.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A brief discussion on MySQL select optimization solution
  • MySQL select results to perform update example tutorial
  • Solve the problem that MySQL read-write separation causes data not to be selected after insert
  • How MySQL Select Statement is Executed
  • Implementation of MySQL select in subquery optimization
  • MySQL learning notes: complete select statement usage example detailed explanation
  • MySQL select, insert, update batch operation statement code examples
  • Explanation of mysql transaction select for update and data consistency processing
  • The difference between Update and select in MySQL for single and multiple tables, and views and temporary tables
  • Detailed explanation of the use of MySQL select cache mechanism
  • Summary of Select usage in MySql database
  • How a select statement is executed in MySQL

<<:  The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands

>>:  Detailed examples of variable and function promotion in JavaScript

Recommend

Example code of how CSS matches multiple classes

CSS matches multiple classes The following HTML t...

How to deploy LNMP architecture in docker

Environmental requirements: IP hostname 192.168.1...

Memcached method for building cache server

Preface Many web applications store data in a rel...

Summary of events that browsers can register

Html event list General Events: onClick HTML: Mous...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

vue3 custom directive details

Table of contents 1. Registering custom instructi...

Build Tomcat9 cluster through Nginx and realize session sharing

Use Nginx to build Tomcat9 cluster and Redis to r...

How to publish static resources in nginx

step Place the prepared static resource files in ...

Summary of MySQL Architecture Knowledge Points

1. Databases and database instances In the study ...

Web Design Experience

<br />The author used to be a novice in web ...

Detailed explanation of the method of comparing dates in MySQL

If there is a table product with a field add_time...

5 Simple XHTML Web Forms for Web Design

Simple XHTML web form in web design 5. Technique ...

The easiest way to install MySQL 5.7.20 using yum in CentOS 7

The default database of CentOS7 is mariadb, but m...