[MySQL 45 lecture notes] 01. How is a SQL query executed?

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

This series is mainly aimed at "45 Lectures on MySQL Actual Combat". After reading each lecture, repeat the content of this chapter in your own language, which can also be called reading notes. Let your brain memorize the content, and there is still a big difference between the memory of the content and the content that you have said.

This lecture is about query sentences. Each operation can be divided into reading and writing in general. Reading is query, which is also the content described in this lecture, and writing can also be regarded as update, which is described in the next lecture. Content, different operations, the corresponding implementation of the bottom layer are different, and the update involves the log. In order to explain how a query statement is executed, we must first have a clear understanding of the MySQL framework.

MySQL is mainly divided into the Server layer and the storage engine layer. The Server layer contains connectors, query caches, analyzers, optimizers, and executors. In some places, a connection layer is also provided. The storage engine layer is plug-in. That is to say, it is like a slot, plugged out and plugged in. The familiar storage engines are InnoDB and MyISAM. Like Ali, he created a storage engine himself and "plugged in" to use it. So one of the advantages of this plug-in type is that it is easy to expand.

Each part is introduced below:

Connector

As the name implies, it is responsible for the connection. You must connect to the database before executing the SQL statement. Enter the user password to verify that it is correct. Each user has its own authority. When the authority is modified, the connection is invalid for this connection. At that time, the permission read out in the permission table will be used as the permission of this connection, and the modified one will be effective when the connection is made the next time.

The connection is divided into long connection and short connection. Long connection means not disconnecting after execution. Short connection means opening a connection for the next query after executing several queries. Establishing a connection each time is a very complicated process, so we recommend Use long connections.

But the long connection leads to a problem that the memory usage is too large for a long time, because the memory is released when the connection is disconnected. If the memory usage is too large, it will cause OOM and the system restarts abnormally. Our solution is to disconnect the connection regularly. Or re-initialize the resource, and re-initialize the resource is not the same as connecting again.

Query cache

After the connection is established, we first go to the query cache to see if there is any result of the previous execution of the SQL statement, and if there is, return directly. This is similar to Redis. There is no need to perform the following operations, which improves the execution speed, but We do not recommend the use of query cache, even after MySQL8.0 directly deleted the module, because as long as the table is updated, the cache of this table will be emptied, and frequent operations will degrade performance.

The query cache is mainly used for tables that are not frequently changed.

Analyzer

If the query cache is not hit, the SQL statement will be executed seriously below. The first critical point is to analyze the SQL statement, because the SQL statement itself is a string, and the analyzer needs to perform lexical analysis and grammar analysis on it. Analysis, lexical analysis is to determine which are commands, which is the name of the table, grammatical analysis is to see whether the command is written correctly, whether the table exists, and whether the column exists. If there is an error, an error will be reported. The Unknow column... and you have an error in your SQL syntax that we often see happen in this layer.

Optimizer

After the analysis is finished, let's check whether there is any optimization. When join connection, which table should we check first? So which one is the choice for so many indexes? This is all determined by the optimizer to maximize performance.

Actuator

Finally, come baby, the executor and the storage engine are matched to complete the execution operation. We need to remember to determine whether the operation is authorized to execute in the executor. If there is permission, open the table to continue execution. When the table is opened, the executor will use the interface provided by the engine according to the engine definition of the table. So the engine is for the table, not the database.

As we said before to determine whether the column exists, it actually occurs in the analyzer. The column is a structure that is determined when the table is created. There is no need to open the table to view the actual data.

If there is a query cache and misses, the result of the last execution will be updated to the query cache.

This is the execution process of a SQL query statement. After reading the content of this lecture, I believe that I have a general understanding of the structure of MySQL. There is a long way to learn, and I will walk with you.

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/113836050
Recommended