If you want to learn MySQL in depth, you should start from the macro architecture. In this article, we will learn the process of executing MySQL query statements. I hope it will be helpful to everyone!
The MySQL version of this article is 8.0.18
The function of the parser is to perform the following work on the SQL statement sent from the client:
The parser mainly checks the grammar and lexicon, but if the grammar and lexicon are correct, but the table , the field does not exist, then this SQL statement cannot be executed correctly.
So the role of the preprocessor is:Semantic parsing, to determine whether the semantics of the parse tree is correct and whether tables and fields exist. After preprocessing, a new parse tree will be obtained.
Query optimizer structure
The execution method of a SQL statement in MySQL is as follows Although the same results will be obtained in the end, there aredifferences in overhead. The specific execution method chosen is determined by the query optimizer. For example:
The query optimizer is a cost-based optimizer. Its working principle is to evaluate various execution plans based on the parse tree. The cost required for the execution method,will eventually get an execution plan with the minimum cost as the final solution.
However, this execution method with the smallest overhead is not necessarily the optimal execution method. For example, an index should be used, but a full table scan is performed. Although there are two words "optimization" in the query optimizer, this optimization is not omnipotent. In many cases, it is more necessary to consider whether the SQL statement is written reasonably.
Logical query optimization is mainly responsible for performing some relational algebra to optimize SQL statements, thereby making SQL statement execution more efficient
We can use several cases to briefly understand logical query optimization
Subquery merging
Before merging
SELECT * FROM t1 WHERE a1<10 AND ( EXISTS(SELECT a2 FROM t2 WHERE t2.a2<5 AND t2.b2=1) OR EXISTS(SELECT a2 FROM t2 WHERE t2.a2<5 AND t2.b2=2) );
After merging
SELECT * FROM t1 WHERE a1<10 AND ( EXISTS(SELECT a2 FROM t2 WHERE t2.a2<5 AND (t2.b2=1 OR t2.b2=2) );
Merge multiple subqueries by merging query conditions, and reduce multiple connection operations to a single table scan and a single connection
Equivalent predicate rewriting
Like the familiar like fuzzy query, % is written after the condition before the index range query is performed. In fact, this is the credit of the query optimizer
Assume that the conditions used are all indexed, before rewriting
SELECT * FROM USERINFO WHERE name LIKE 'Abc%';
After rewriting
SELECT * FROM USERINFO WHERE name >= 'Abc' AND name < 'Abd';
This is why the answer to index range query
Conditional simplification
Conditional simplification is also used Some equations and algebraic relationships are used to achieve simplification
((a AND b) AND (c AND d))
is simplified toa AND b AND c AND d
col1 = col2 AND col2 = 3
is simplified tocol1 = 3 AND col2 = 3
col1 = 1 2
Simplification Forcol1 = 3
The above parameters are explained as follows:
For information on index cost calculation, please refer to this article:Why did MySQL query choose to use this index? ——Based on MySQL 8.0.22 index cost calculation
The execution plan is the product of the query optimizer and will eventually be handed over to the storage engine for execution . The execution plan can help us know how MySQL will execute this SQL statement.
Use theexplain
keyword to view the execution plan of the SQL statement, and you can get the following information:
The MySQL server stipulates specifications for how data is stored, extracted, and updated. This specification is implemented by storage engines. Different storage engines have different implementation methods, so different storage engines will present their unique functions and characteristics. The most commonly used storage engines are InnoDB and MyISAM
Let’s briefly talk about the characteristics of these two storage engines
InnoDB:
MyISAM
The storage engine will not be expanded on for the time being, and will continue to be interspersed with their comparisons in other articles, as well as details. Analyze the process of updating data in InnoDB
In the past, I only knew how to write SQL statements on the client software, click to execute, and get the data
Now I finally understand that after a query statement is passed into the MySQL server, it needs to go through this series of operations
The parser checks the syntax and lexicon of this SQL statement. , if there are no errors, it will be split into nodes according to keywords, and finally a parse tree will be formed
The preprocessor will check the semantics of the SQL statement and check whether the SQL statement is ambiguous , fields, etc., to form a new parse tree
The query optimizer gets the various execution plans generated by this parse tree, and obtains them after logical query optimization and physical query optimization An execution plan with minimal overhead
The execution engine gets this execution plan and calls the storage engine interface
The storage engine processes data according to the execution plan Query, the query will query and call some interfaces of the file system in the operating system, complete the data query, and finally return to the client
[Related recommendations:mysql video tutorial]
Cost evaluation formula | |
---|---|
N_page * a_page_IO_time N_tuple * a_tuple_CPU_time | |
C_index N_page_index * a_page_IO_time |
The above is the detailed content of MySQL learning to talk about query statement execution process. For more information, please follow other related articles on the PHP Chinese website!