Chapter 18: Logical Architecture

1. Analysis of logical structure

1.1 The server processes the client request

①MySQL is a typical C/S architecture, and the server side uses mysqld

②The client process sends SQL statements to the server process, and the server process sends processing results to the client process.

Query Request Demo Diagram

Query request detailed diagram

 1.2 Layer 1: Connection layer

① Before the client accesses the MySQL server, the first thing is to establish a TCP connection.

②After the three-way handshake establishes the connection successfully, the MySQL server authenticates the account number and password transmitted by TCP. On error, the client ends execution. If it is correct, find out the authority of this account from the authority table.

③When the TCP connection receives the request, it will allocate a thread in the thread pool to interact with this client. Each connection obtains a thread from the thread pool, eliminating the overhead of creating and destroying threads.

1.3 Layer 2: Service Layer

①SQL interface

Receive the user's SQL command and return the user's query result. select *** from

② Parser

Perform syntax analysis and semantic analysis on SQL statements in the parser. Decompose SQL statements into data structures, syntax trees. Enrich the query syntax tree according to the data dictionary, and verify whether the client has the authority to query.

③Query optimizer

The SQL statement uses the optimizer to determine the execution path of the SQL statement and generate an execution plan.

The execution plan indicates which index queries to use, the order of joins between tables, the storage engine, and returns the results to the user.

Use "selection-projection-connection", first select according to the where statement, then project through the select query condition, and finally connect the two query conditions to generate the final result.

④Query cache (deleted in 8.0)

Cache the execution result of a select statement, and the query cache can be shared among different clients.

1.4 Layer 3: Engine Layer

Responsible for the storage and extraction of MySQL data, and perform operations on the underlying data maintained at the physical server level.

1.5 Summary

①Connection layer: The client establishes a connection with the server, and the client sends SQL to the server

② Service layer: Query SQL statements, regardless of the storage method of database files

③Engine layer: dealing with database files, responsible for data storage and reading

Two, SQL execution process

2.1 SQL execution process in MySQL

 

MySQL query process:

1. Query cache : When the server finds this SQL statement in the query cache, it will directly return the result to the client. If not, access the parser stage. The efficiency is not high, and it is deleted in MySQL8.0.

2. Parser : Perform syntax analysis and semantic analysis on the SQL statement in the parser.

The analyzer first performs "lexical analysis" to identify keywords and table names from the string of the SQL statement.

Then do "syntactic analysis" to see if the SQL statement satisfies the grammar, and the statement correctly generates a syntax tree.

3. Optimizer : Determine the execution path of the SQL statement in the optimizer, such as full table retrieval or index retrieval. A query can be executed in multiple ways, and the plan with the shortest execution time is found.

Physical query optimization: optimize through indexes and table joins

Logical query optimization: change to a better way of writing SQL queries

4. Actuator:

Determine whether this query has permission.

Execute the SQL query and return the result. If caching is set, the query results are cached.

【Summarize】

SQL statement—query cache—parser—optimizer—executor.

 Three, the database buffer pool buffer pool

3.1 Buffer Pool VS Query Cache

1. Buffer pool

① concept

The InnoDB storage engine uses pages to manage storage space, and operations in memory are much more efficient. The DBMS will apply for memory as a data buffer pool.

Reduce the time of direct I/O to the disk.

②Cache principle

Prioritize loading frequently used data into memory

③The read-ahead feature of the buffer pool

When we use some data, there is a high probability that we will use some surrounding data and use the "read-ahead" mechanism to load in advance to reduce future disk I/O operations.

2. Query cache

Save the query results in advance, KV. K is the select statement, and V is the query result.

3.2 How the buffer pool reads data

When the database performs a page read operation, first determine whether the page is in the buffer pool, and read it directly if it exists. If it does not exist, it is loaded from disk or memory into the buffer pool.

[Question] If the SQL statement is executed to update the data in the buffer pool, will it be synchronized to the disk immediately?

The database will be refreshed to the disk at a certain frequency, and it will not be written back immediately after each update. When the buffer pool is not enough, dirty pages (modified pages, infrequently queried) will be written back to disk to release dirty pages.

3.3 View/set the size of the buffer pool

1. View the buffer pool size 128MB

show variables like 'innodb_buffer_pool_size';

2. Modify the buffer pool size

set global innodb_buffer_pool_size = 268435456;

3.4 Update data flow

When updating a certain data, first load the data from the memory or disk to the Buffer Pool, then modify the data in the memory, and finally flush it to the disk periodically.

If there is a crash when flashing to disk, the data will be rolled back to Redo Log&Undo Log

 

 

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/131204803