The process of executing SQL in MySQL

1. Usually sql execution process

insert image description here

  • When the user initiates a request to the business server, when executing the SQL statement, it first obtains a connection from the connection pool, and then executes the query on the MySQL server.

1.1 Question 1: Who handles network requests in MySQL?

Who is responsible for the msyql server to listen for this network request from this connection? Who is responsible for reading the data out of the network connection?

  • In fact, everyone knows that a network connection must be allocated a thread to handle, and a thread to monitor and read the request data, such as reading and parsing a SQL statement sent from the network connection, as shown in the following figure:
    insert image description here

1.2 Question 2: How does MySQL execute sql statements?

  • Then let's think about it, when the worker thread inside mysql reads an SQL statement from a network connection, how will the SQL statement be executed at this time?
  • If you want to execute this sql to complete the underlying data addition, deletion, modification and query, it is actually an extremely complex task. Mysql first provides a component, which is the SQL interface (SQL Interface), which is a set of interfaces for executing SQL statements, which is specially used to execute the SQL statements that we send to MySQL for adding, deleting, modifying and checking.
  • Therefore, after the mysql worker thread receives the SQL statement, it will be transferred to the SQL interface for execution, as shown in the following figure:
    insert image description here

1.3 Query parser

How does MySQL read and understand these SQL? For example, there is such an SQL now, we can process it with human brain, as long as people who understand SQL syntax, will immediately understand what it means. But MySQL is a database management system, and he cannot directly understand these SQL statements! This is where a key component comes into play: the query parser.
The query parser (Parser) is responsible for parsing SQL statements. For example, the above SQL statement can be disassembled into the following parts:

  1. To query data from the "users" table
  2. Query the row of data whose "id" field is equal to 1
  3. For the line of data queried, we need to extract the three fields of "id, name, age".
    The so-called SQL parsing is to parse the sql statement we wrote according to the sql grammar rules according to the established sql grammar, and then understand the sql statement What to do, as shown below:
    insert image description here

1.4 Query Optimizer

When the parser understands what SQL is going to do, it does not execute it immediately, but selects an optimal query path through the query optimizer. Take the sql query above as an example to see what the so-called optimal query path is.
To accomplish this, we have the following query paths:

  • Query path 1 : Directly locate the row of data where the "ID" field in the "users" table is equal to 1, and then take the three field values ​​of "id, name, age" in that row of data.
  • Query path 2 : First find out the values ​​of the three fields "id, name, age" of each row of data in the "users" table, and then pass the row of data with the "id" field equal to 1 from this batch of data
    The above are two implementation paths of the simplest SQL statement. To accomplish the goal of this SQL statement, both paths can be achieved, but which one is better? Obviously, the first query path is better.

Therefore, the query optimizer generates a query path tree for the complex SQL statements you write with dozens, hundreds or even thousands of lines, and then selects an optimal query path from it. It is equivalent to telling the sql executor what operations to perform according to what steps and order, and then completing the SQL statement step by step.
insert image description here

1.5 Storage Engine

The database is just a system written in a programming language, and after it is started, it is also a process that executes various codes in the pool, that is, the things we mentioned above. So for the database, our data is either in memory or in disk files, nothing special! So let's think about it, assuming that some of our data is stored in memory, and some are stored in disk files as shown in the figure below.

insert image description here
So the question is, do we update the memory data first when we execute it? Or disk data? If we update the disk data first, which disk file is queried first, and which disk file is updated?
This is where the storage engine is needed. The storage engine actually executes SQL statements. It will follow certain steps to query memory cache data, update disk data, query disk data, etc., and perform a series of data operations, as shown in the following figure:
insert image description here
In the MySQL architecture design, SQL The interface, SQL parser, and query optimizer are all a set of components used. But the storage engine is like a plug-in, which supports a variety of storage engines, such as our common InnoDB, MylSam, Momery, etc. We are free to choose which storage engine is responsible for specific SQL execution. But now everyone generally uses InnoDB as the storage engine for MySQL.

1.6 Actuators

After reading the storage engine, we know that the storage engine can help us access the data on the memory and disk, so who will call the interface of the storage engine? In fact, we have also missed the concept of an executor. This executor will call the interface of the storage engine according to the execution plan selected by the optimizer, and execute the logic of the SQL statement in a certain order and steps .

For example, for example, the executor may first call an interface of the storage engine to obtain the first row of data in the "usrs" table, and then determine whether the value of the "id" field of this data is equal to a value we expect, If not, then continue to call the interface of the storage engine to get the next row of data in the "users" table.
Based on the above ideas, the executor will go to a set of execution plans generated by our optimizer, and then constantly call various interfaces of the storage engine to complete the execution plan of the SQL statement, which is roughly constantly updating or extracting some data out. As shown below:

insert image description here

1.7 Summary

Let's summarize the execution process of MySQL:

  1. The user initiates a request to the tomcat server, and the thread in the tomcat server starts to execute the sql statement
  2. First, the thread obtains a connection from the connection pool of the database and establishes a connection to the MySQL database server
  3. After the MySQL worker thread receives the SQL statement, it transfers it to the SQL interface for execution.
  4. The query parser disassembles the SQL syntax and understands what the SQL does
  5. The query optimizer selects the optimal query path and generates the execution sequence and steps of SQL
  6. It is handed over to the underlying storage engine interface for execution. He follows certain steps to query memory data, update disk data, etc.
    The execution process is shown in the following figure:
    insert image description here

Guess you like

Origin blog.csdn.net/zht245648124/article/details/126686293