Article directory
Column catalog please click
Introduction
Let's look at this picture first,
it can be simplified as follows
query process
- First of all, MysQL is a typical C/S architecture, that is,
Client/Server
the architecture used by server-side programsmysqld
. - Regardless of the communication method used by the client process and the server process, the final effect is: the client process sends a piece of text (SQL statement) to the server process, and the server process sends a piece of text to the client process after processing (processing result) .
- Specifically, we can see the following figure
- We will find that the whole process can be divided into three major parts
- connection management
- Analysis and optimization
- storage engine
There is no query cache step in mysql8.0. The files on the physical disk in the file system are first loaded into the storage engine.
- Therefore, the above figure can be roughly divided into the following three layers.
- Let's go over these modules in detail
connection management
-
Each successful client connection will have a thread in the server process, and the server will cache the thread, which can only run in a certain CPU in turn, so there is no need to create and destroy threads
-
Accessing MysQL through sDK essentially interacts with MysQL through the MysQL protocol on a TCP connection.
Connectors
- Connectors refer to the interaction with MYSQL in different languages.
- Before the system (client) accesses the MySQL server, the first thing it does is to establish a TCP connection. After the three-way handshake establishes the connection successfully, the MysQL server performs identity authentication and permission acquisition on the account password transmitted by TCP.
- After the TCP connection receives the request, it must be assigned to a thread to interact with the client. So there will be a thread pool to go through the following processes. Each connection obtains threads from the thread pool, eliminating the overhead of creating and destroying threads, as shown in the figure below
Parsing and optimizing queries
This belongs to the second layer of service layer
- At this layer, the server parses the query and creates a corresponding internal parse tree, which is optimized accordingly
- For example, determine the order of query tables, whether to
use indexes, etc., and finally generate corresponding execution operations. - If it is a SELECT statement, the server will also query the internal cache.
- For example, determine the order of query tables, whether to
SQL Interface
SQL interface
- It is used to receive the user's SQL command and return the result that the user needs to query. The query statement is to call
SQL Interface
- It supports multiple SQL languages such as DML, DDL, stored procedures, views, triggers, and custom functions.
Parser
parser
- In the parser, the SQL statement is parsed and semantically analyzed , the SQL statement is decomposed into a data structure, and this structure is passed to the subsequent steps. The subsequent transmission and processing of the SQL statement is based on this structure.
- If an error is encountered in the decomposition, it means that the SQL statement is unreasonable.
- When the SQL command is passed to the parser, it will be verified and parsed by the parser, and a syntax tree will be created for it, and the query syntax tree will be enriched according to the data dictionary, and it will be verified whether the client has the authority to execute the query.
- After the syntax tree is created, MYSQL will also optimize the syntax of the SQL query and rewrite the query.
Optimizer
query optimizer
- After the SQL syntax is parsed and before the query, the query optimizer will be used to determine the execution path of the SQL statement and generate an execution plan .
- This execution plan indicates which indexes should be used for query (full table index or index retrieval), what is the order of links between tables, and finally, the steps in the execution plan will call the method provided by the storage engine to actually execute the query, and will Query results are returned to the user
query strategy
He is using 选取-投影-链接
the strategy of the query
SELECT id,name FROM student WHERE gender = '男';
- He will first select
WHERE
the sentence , - Then perform attribute projection based on id and name
- Link the above two query conditions to generate the final query result
Caches & Buffers
Query Cache Component
- MySQL maintains some Cache and Buffer internally
- This caching mechanism is composed of a series of small caches. Such as table cache, record cache, key cache, permission cache, etc., and can be shared between different clients
- After MySQL5.7.20, it is recommended to use query cache and delete it in MySQL8.0
storage
this belongs to the third layer
- The storage engine layer is really responsible for the storage and extraction of data in MYSql, and performs operations on the dungeon data maintained at the physical server level.
- Storage Engine Detailed Click
storage layer
- All the data, the content of each row of the database, the table, and the index are stored on the file system in the form of files, and complete the interaction with the storage engine.
- Some storage engines do not support direct management of raw devices using file systems, such as InnoDB.
- The file system can use local disks, or various storage systems such as DAS, NAS, and SAN
reference