PrefaceWe need to retrieve certain data that meets the requirements from the database. It is easy for us to write SQL such as Select ABC FROM T WHERE ID = XX. So what does the database do when we send such a request to the database? Today we take MYSQL as an example to reveal the query process of the MySQL database and let everyone know some parts of the database. MYSQL Architecture mysql architecture MySQL can be mainly divided into the server layer and the storage engine layer. The server layer includes connectors, query caches, analyzers, optimizers, executors, etc. All cross-storage engine functions are implemented in this layer, such as stored procedures, triggers, views, functions, etc. There is also a general log module, the binlog log module; The storage engine layer is responsible for storing and retrieving data. Its architecture mode is plug-in-based and supports multiple storage engines such as InnoDB, MyISAM, and Memory. The most commonly used storage engine now is InnoDB (which supports transactions), which has become the default storage engine since MySQL version 5.5.5. ConnectorsThe connector is mainly responsible for user login to the database and user identity authentication, including verification of account passwords, permissions and other operations. If the user password is incorrect, you will receive an "Access denied for user" error and the client program will terminate execution. If the user account password has been approved, the connector will query all permissions of the user in the permission table. All subsequent permission logic judgments in this connection will rely on the permission data read at this time. In other words, as long as the connection is not disconnected, the user will not be affected even if the administrator modifies the user's permissions. Query cacheAfter the client establishes a connection with the server, MySQL will first query the cache when executing a query statement to verify whether this SQL has been executed before. Previously executed statements and their results are cached directly in memory in the form of key-value pairs. The key is the query statement and the value is the query result. If your query can find the key directly in this cache, then the value will be returned directly to the client. If there is no hit, subsequent operations need to be performed, and the results will be cached after completion to facilitate the next call. After seeing this, will your eyes light up? Will you have the urge to make good use of this great function? In fact, it is not recommended to use query cache here. The query cache expires very frequently. As long as a table is updated, all query caches on this table will be cleared. Therefore, it is very likely that you have gone to great lengths to save the results, but they are cleared by an update before you can use them. For databases with heavy update pressure, the query cache hit rate will be very low. Unless it is a table that will not be updated for a long time, such as a system configuration table, but wouldn’t it be better for us to put this kind of system configuration on the configuration platform? The query cache function has been deleted in MYSQL8.0. The official also believes that this function has few actual application scenarios, so it was simply deleted. AnalyzerIf Mysql does not hit the query cache, it will enter the analyzer, which is mainly used to analyze what the SQL statement is for. The analyzer is mainly divided into the following two steps:
The lexical analyzer breaks down the entire query statement into various tokens, and the syntax analyzer converts the "various tokens" into combinations that are meaningful to MySQL based on the defined system language. Finally, the system generates a syntax tree (AST), which is the data structure that the optimizer relies on. OptimizerAfter the analyzer, MySQL knows what you are going to do. Before execution begins, it must first be processed by the optimizer. Why do we need an optimizer?
In short, the optimizer modifies the shape of the syntax analysis tree, converts the syntax analysis tree into a query tree, and determines the execution plan. ActuatorMySQL knows what you want to do through the analyzer, and knows how to do it through the optimizer, so it enters the executor stage and starts executing the statement. When starting execution, it is necessary to first check whether the user has the permission to execute the query. If not, an error message indicating that the user does not have permission will be returned. If there is permission, the engine's interface will be called and the result of the interface execution will be returned. Statement Analysis Let's analyze the execution process of MYSQL query with the following real SQL query statement: select id,name,sex,phoone from user t where t.age='26' and t.account='javadaily'
a. First query the user with account=javadaily, then check whether age is equal to 26 b. First find the user with age=26, then query the user with account=javadaily
SummarizeThis is the end of this article about understanding the execution process of MySQL query statements. For more relevant MySQL query execution process content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Use vue3 to implement a human-cat communication applet
>>: Page Speed Optimization at a Glance
Table of contents JS obtains the .txt file conten...
Table of contents cache Cache location classifica...
Table of contents introduce Prepare Download syst...
1. Install cmake 1. Unzip the cmake compressed pa...
It is mainly the configuration jump of the if jud...
The hyperlink a tag represents a link point and i...
This article shares the installation and configur...
Table of contents Early creation method Factory P...
Preface This control will have a watermark at the...
nohup Command When using Unix/Linux, we usually w...
Recently, there is a particularly abnormal busine...
Table of contents 1. Signal List 1.1. Real-time s...
Table of contents 1. Digital Enumeration 2. Strin...
The display effects on IE, Fir...
Table of contents The difference between hash and...