MySQL Logical Architecture_A Preliminary Understanding of Logical Architecture

What is a logical architecture?

First of all, MySQL is a typical C/S architecture, that is, Client/Server architecture, and mysqld used by server-side programs.

The effect to be realized by the client process and the server process is: the client process sends a section of text (SQL statement) to the server process, and the server process sends a section of text (processing result) to the client process after processing.

What does the server process do to the request sent by the client process to produce the final processing result?

Here we take the query request as an example to show:
query request
the part inside the big dotted line box is the logical structure

  • Connection management is used to process requests and connections, and check whether users meet database permissions
  • Analysis and optimization is the analysis and optimization of SQL statements
  • It is used to call the search operation of the realization data

The specific expansion of the logical architecture can be expressed in the following form:
insert image description here
Let’s take a look at the specific expansion


Connectors

Connectors, refers to the interaction with SQL in different languages . MySQL is first and foremost a network program that defines its own application layer protocol on top of TCP. So to use MySQL, we can write code, establish a TCP connection with MySQL Server, and then interact according to its defined protocol. Or a more convenient way is to call the SDK, such as Native C API、JDBC、PHPMysQL Connector in various languages, or through ODBC. However, accessing MySQL through the SDK essentially interacts with MySQL through the MySQL protocol on a TCP connection.


The first layer: connection layer

Realize the establishment of a connection between the client and the server, and the client sends SQL to the server.

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 .

  • If the user name or password is incorrect, an Access denied for usrerror will be received, and the client program will end execution
  • If the user name and password authentication is passed, the permissions owned by the account will be found out from the permissions table and associated with the connection, and the subsequent permission judgment logic will depend on the permissions read at this time

Then let's think about a question

  • Can a system only establish one connection with the MySQL server? Can only one system establish a connection with the MySQL server?

Of course not, multiple systems can establish connections with the MySQL server, and each system must establish more than one connection. Therefore, in order to solve the problem of resource exhaustion and performance degradation caused by unlimited creation of TCP and frequent creation and destruction of TCP. There is a dedicated TCP connection pool in the MySQL server to limit the number of connections, and the long connection mode is used to multiplex TCP connections to solve the above problems.

insert image description here
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 gets a thread from the thread pool, eliminating the overhead of creating and destroying threads. We have summarized these contents into the connection management component
of MySQL . Therefore, the responsibility of connection management is to be responsible for authentication, connection management, and access to permission information.


The second layer: service layer

It also becomes the SQL layer, which performs query processing on SQL statements, regardless of the storage method of the database file.

The second-tier architecture mainly completes most of the core service functions , such as SQL interface, and completes cached queries , SQL analysis and optimization, and execution of some built-in functions . All cross-storage engine functions are also implemented at this layer, such as procedures and functions.
At this layer, the server will parse the query and create a corresponding internal parsing tree, and optimize it accordingly : such as determining the order of the query table, whether to use the index, etc., and finally generate the corresponding execution operation .
If it is a SELECT statement, the server will also query the internal cache. If the cache space is large enough, the performance of the system can be greatly improved in the environment of solving a large number of read operations.
insert image description here

It includes SQL Interface, Parser, Optimizer, Caches & Buffers in the above figure

SQL Interface: SQL interface

  • It is used to receive the user's SQL command and return the result that the user needs to query . For example, SELECT ... FROM is to call SQL Interface
  • MySQL supports DML (Data Manipulation Language), DDL (Data Definition Language), stored procedures, views, triggers, custom functions and other SQL language interfaces

Parser: Parser

  • Perform syntax analysis and semantic analysis on SQL statements in the parser . Decompose the SQL statement into a data structure , and pass this structure to the subsequent steps, and 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 statement is parsed and before querying, 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 retrieval or index retrieval), what is the connection order between tables, and finally, the method provided by the storage engine will be called according to the steps in the execution plan to actually execute the query , and will The query results are returned to the user.
  • It uses a "pick-projection-join" strategy for queries.

Caches & Buffers: query buffer components

  • MySQL maintains some Cache and Buffer internally. For example, Query Cache is used to cache the execution result of a SELECT statement (if the corresponding query result can be found in it, then there is no need to go through the whole process of query parsing, optimization and execution, and the The result is fed back to the client)
  • This caching mechanism is composed of a series of small caches. Such as table cache, record cache, key cache, permission cache, etc.
  • This query cache can be shared among different clients.
  • Starting with MySQL 5.7.20, the query cache is deprecated and has been removed in MySQL 8.0 .

The third layer: engine layer

The role is to be responsible for the storage and extraction of data in MySQL, and to deal with database files

  • MySQL is different from other databases. Its architecture can be applied in many different scenarios and play a good role , which is mainly reflected in the architecture of the storage engine. The plug-in storage engine architecture separates query processing from other system tasks and data storage and extraction. This architecture can select an appropriate storage engine according to business needs and actual needs . At the same time, the open source MySQL also allows developers to set their own storage engines .
  • This efficient, modular architecture offers enormous benefits to those who wish to specialize in specific application needs, such as data warehousing, transaction processing, or high-availability situations, while enjoying the advantages of using a set of storage independent of any interfaces and services engine.
  • The plug-in storage engine layer (Storage Engines) is " really responsible for the storage and extraction of data in MySQL, and performs operations on the underlying data maintained at the physical server level ", and the server communicates with the storage engine through the API. Different storage engines have different functions, so we can choose according to our actual needs .

Through show engines;the statement, you can see the following storage engines in the database:
insert image description here


In summary, the MySQL structure diagram at the beginning can be simplified into the following three-tier structure:
insert image description here

Guess you like

Origin blog.csdn.net/z135733/article/details/129476268