The process of parsing a SQL statement in MySQL

How are SQL statements executed? Let's start from the beginning

After writing so many CURDs, do you really understand the execution process of SQL?

If your knowledge of MySQL is still a black box, then quickly join me to see how it is inside

In order to enable most readers to understand, I will try my best to explain in vernacular

Not much to say, let’s get to the topic~

1. The connection pool between MySQL and the client

For the client, we all know that the connection between the Java system and the database is MySQL驱动realized by means of the following, as shown below

Insert picture description here

We all know that frequently establishing and destroying connections is a thing that consumes system resources, so 连接池it came into being, like the following

Insert picture description here

In the same way, we think from the perspective of the database, and we also need to maintain a connection pool to establish a connection with the Java system, as shown in the following figure

Insert picture description here

It is not difficult to see from the above that the first step for MySQL to execute SQL statements is连接池

2. Analyze and optimize SQL statements

When a connection in the connection pool in MySQL receives a request, it will be handed over to a thread that listens to the request, and the SQL statement is handed over to him for processing, as shown below

Insert picture description here

3. SQL interface

Thread can not directly process SQL, it will hand over SQL for SQL接口execution

Insert picture description here

4. SQL parser

As we all know, even if you are not a programmer, as long as you know the words in the SQL statement, you can basically understand the meaning of the SQL statement, that is, the SQL statement is so easy to understand, so MySQL has spent a lot of effort to turn it into a computer understandable SQL解析器Yes , to complete this function, you first need to parse the SQL statement, as shown in the following figure

Insert picture description here

5. Query optimizer

After being parsed by the SQL parser, we extracted key information from the SQL statement, such as id, name, and other key information. We now know which row in that table our target data is, and so on. , For example, if we want to inquire user表中id=10的那一行的name,age信息,并且这个表中有30000个数据, the question now is how should we make inquiries? For example, should I first find out the data of the row with id=10 from 30,000 data and then filter the field to be queried, or filter the field first and then query the one with id=10 from the 30,000 data? (This is just an example and not a principle). In practice, there may be many query methods. We call them 查询路径, they form one 查询路径树. After we have the query path tree, we need to choose an optimal path from it. We 查询优化器will handle the above work

Insert picture description here

6, call the storage engine

Now, we have got the most useful query path, as if we already know how to do it, the next step is very simple, just execute the obtained query path, this step, we 存储引擎leave it to deal with, why? We can understand by analysis. First of all, we all know that data is stored in disk or in memory. How do we know which data is in memory and which data is in disk? In order to solve this problem, we need a special processing data query, update, add and delete operations 存储引擎, as shown in the following figure

Insert picture description here

7. Actuator

After understanding 存储引擎, we come back to think about a question, storage engine can help us to access the data on the memory and disk, and there are many types of storage engines, so who will call the storage engine interface? The function of the query optimizer we mentioned earlier is only to generate the optimal query path, and it is not responsible for calling the storage engine. In fact, we missed one 执行器. Its main responsibility is to continuously call various interfaces of the storage engine to complete the execution of SQL statements according to the execution plan generated by the optimizer.

Insert picture description here

Speaking of this, you must have a preliminary understanding of the SQL statement execution process, I will update later 存储引擎是用来作什么的, if you have any questions, please comment and leave a message

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/110017451