What are the parts of MySQL?
- Connector: management connection, permission verification.
- Analyzer: lexical analysis, syntax analysis.
- Optimizer: execution plan generation, index selection.
- Executor: operates the storage engine and returns results.
Storage engine: stores data and provides read and write interfaces.
Related learning recommendations: mysql video tutorial
In the first step, we will first connect to the MySQL database, and now we will connect the connector. The connector is responsible for establishing a connection with the client, obtaining permissions, maintaining and managing the connection.
mysql -h $ip -u root -p
After establishing the connection, we can use the SELECT statement, and the execution logic will come to the second step: query cache. MySQL will now query the cache to see if this statement has been executed before, and if so, return it directly. This module has been removed since MySQL 8.0.
If there is no query cache, MySQL will start to analyze what we want to do from here, and it needs to analyze the SQL statements we write. The analyzer will first do lexical analysis to identify the string and what it represents. Then perform syntax analysis to determine whether there are errors in the SQL statements we wrote. If there are errors, an error will be thrown.
After passing the analyzer, MySQL knows what you are going to do. At this time, the optimizer will decide which solution to use based on the table structure and statement purpose.
MySQL knows what we want to do through the analyzer, and knows how to do it most efficiently through the optimizer. Then you can enter the executor and actually execute the SQL statement.
select * from users where name = ‘operator'
Assuming that there is no index on the name field in the users table, then the executor calls the InnoDB engine interface to get the first row and determines whether the name is equal to the operator. If not, skip it. If so, put it in the result. concentrated. Then call the engine interface to fetch the next row, and repeat the same logical judgment until the last row of the table is fetched. Finally, the result set is returned to the client.
The above is the detailed content of How is a SQL query statement executed?. For more information, please follow other related articles on the PHP Chinese website!