1. Components of Mysql
Mysql consists of SQL interface, parser, optimizer, cache, and storage engine.
2. Mysql working principle diagram
##3. Description of each component of Mysql schematic diagram
3-1: connectors
Interact with sql statements in other programming languages, such as php, java, etc.
3-2:Management Serveices & Utilities
System management and control tools
3-3、Connection Pool
Manage buffer users Connection, thread processing and other requirements that require caching
3-4. SQL Interface (SQL interface)
Accepts the user's SQL command and returns the results that the user needs to query. For example, select from is to call SQL Interface
3-5, Parser (parser)
When the SQL command is passed to the parser, it will be verified and parsed by the parser.
Main functions of the parser:
a. Decompose the SQL statement into a data structure and pass this structure to subsequent steps. The subsequent transmission and processing of SQL statements are based on this structure
b. If an error is encountered during the decomposition, it means that the sql statement is unreasonable and the statement will not continue to be executed.
3-6. Optimizer (Query Optimizer)
The SQL statement will use the query optimizer to optimize the query before querying (generating multiple execution plans, and finally the database will choose the most optimized plan to execute and return the results as soon as possible) He uses "select-projection" -Join" strategy for querying.
You can understand it with an example: select uid,name from user where gender = 1;
This select query first selects based on the where statement, instead of querying all the tables first. Perform gender filtering
This select query first performs attribute projection based on uid and name, instead of removing all attributes and then filtering
Connect these two query conditions to generate the final query result.
3-7. Cache and Buffer (query cache)
If the query cache has a hit query result, the query statement can directly fetch data from the query cache.
This caching mechanism is composed of a series of small caches. For example, table cache, record cache, key cache, permission cache, etc.
3-8. Engine (storage engine)
The storage engine is the specific subsystem in MySql that deals with files, and it is also the MySQL One of the most unique places.
Mysql's storage engine is plug-in. It customizes a file access mechanism based on an abstract interface of the file access layer provided by MySql AB (this access mechanism is called a storage engine).
4. SQL statement execution process
The database is usually not used directly, but other programming languages call mysql through SQL statements, which is processed by mysql and returned for execution. result. So how does Mysql process the SQL statement after it receives it?
First, the request of the program will interact with it through the connectors of mysql. After the request is received, it will be temporarily stored in the connection pool (connection pool) and processed by the processor (
Management Serveices & Utilities) manage. When the request enters the processing queue from the waiting queue, the manager will throw the request to the SQL interface (
SQL Interface). After the SQL interface receives the request, it will hash the request and compare it with the results in the cache. If there is a complete match, the processing result will be returned directly through the cache; otherwise, a complete process will be required:
(1) The SQL interface is thrown to the subsequent interpreter (Parser). The interpreter will determine whether the SQL statement is correct or not, and if it is correct, it will convert it into a data structure.
(2) After the interpreter is processed, it comes to the optimizer (Optimizer) behind, which will generate multiple execution plans. In the end, the database will choose the most optimized plan for execution and return the results as soon as possible.
(3) After determining the optimal execution plan, the SQL statement can be handed over to the storage engine (Engine) for processing. The storage engine will obtain the corresponding data from the back-end storage device and return it to the original path. to the program.
5. Note
(1) How to cache query data
The storage engine processes the data and returns it to the program At the same time, it will also retain a copy of the data in the cache so that the next same request can be processed more quickly. The specific situation is that mysql will hash the query statement, execution results, etc., and keep them in the cache, waiting for the next query.
(2) The difference between buffer and cache
As you can see from the mysql schematic diagram, there are actually two buffers and caches in the cache. The difference between them:
To put it simply, buffer is a write cache and cache is a read cache.
(3) How to judge whether the required data has been cached in the cache
There may be a misunderstanding here. When processing SQL statements, in order to judge whether the query results have been cached, the entire process will be Go through it again, obtain the execution result, and then compare it with the required one to see if there is a hit. In this way, since no matter whether the query content is cached in the cache, you have to go through the entire process, what is the advantage of caching?
In fact, this is not the case. After the first query, mysql will hash the query statement and query results and keep them in the cache. After the SQL query arrives, it will be hashed in the same way. The two hash values are compared. If they are the same, it is a hit and the query result is returned from the cache; otherwise, the entire process needs to be repeated.
I hope this article can provide some help to students who are learning databases. If there is something wrong in the article, please point it out. Thank you very much!
For more Mysql related questions, please visit the PHP Chinese website: mysql video tutorial
The above is the detailed content of Detailed introduction to the working principle of mysql. For more information, please follow other related articles on the PHP Chinese website!