current position:Home>L01_ How is an SQL query executed?

L01_ How is an SQL query executed?

2022-06-24 09:15:48jackaroo2020

Reading guide

This article mainly adopts a SQL Query statement , Let's have a preliminary understanding of MYSQL Basic framework .

 Insert picture description here

1、MYSQL Logical architecture

mysql> select * from T where ID=10;

Here is MySQL Basic architecture diagram , You can see clearly from it SQL Statements in MySQL The execution process in each function module of .

 Insert picture description here

2、MySQL The framework has several components , What is the role of each ?

The connector
Responsible for and mysql The client establishes a connection , Get user rights , Maintaining and managing connections . The connection command is usually written like this :

mysql>mysql -h$ip -P$port -u$user -p

When the connection is complete , If you don't follow up , This connection is idle , You can show processlist See it in the command . As shown in the figure , Among them Command The column is shown as “Sleep” This line , It means that there is an idle connection in the system . If the client does not move for a long time , The connector will automatically disconnect it . This time is determined by the parameter wait_timeout The control of the , The default value is 8 Hours . The order is :

 mysql>show processlist;
 mysql>show variables like 'wait_timeout';

 Insert picture description here

The query cache
Query requests to access the cache first (key Is the statement of the query ,value Is the result of a query ). Hit direct return . Caching is not recommended , Updates will clear the cache ( Turn off caching : Parameters query_cache_type Set to DEMAND). For the statements that you decide to use the query cache , It can be used SQL_CACHE Explicitly specify , Like the following statement :

 mysql>select SQL_CACHE * from T where ID=10;

It should be noted that ,MySQL 8.0 Version of the query cache directly removed the entire block , in other words 8.0 It's starting to disappear completely .

analysis sql Statement for errors , Lexical analysis and grammatical analysis .
Decide which index to use , More than a table (join) When , Determine the join order of the tables .
Execute statement , First judge whether the user has query permission , Storage engine using table definitions .

mysql> select * from T where ID=10;

For example, the table in our example T in ,ID Field has no index , So the execution process of the actuator is like this :

(1) call InnoDB The engine interface takes the first row of this table , Judge ID Value is 10, If not, skip , If it is, the row will exist in the result set ;

(2) Call the engine interface “ The next line ”, Repeat the same logic of judgment , Until you get to the last row of the table .

(3) The executor returns the record set composed of all the rows that meet the conditions in the traversal process to the client as a result set .

3、Server What are the roles of the storage engine layer and the storage engine layer ?

In general ,MySQL Can be divided into Server Layer and storage engine layer .

Server layer Including connectors 、 The query cache 、 analyzer 、 Optimizer 、 Actuators etc. , cover MySQL Most of the core service functions of , And all the built-in functions ( Such as date 、 Time 、 Mathematics and cryptographic functions ), All cross-storage engine functionality is implemented in this layer , Like stored procedures 、 trigger 、 View etc. .

Storage engine layer Responsible for data storage and extraction . Its architecture pattern is plug-in , Support InnoDB、MyISAM、Memory Wait for multiple storage engines . Now the most commonly used storage engine is InnoDB, It is from MySQL 5.5.5 Version began to be the default storage engine .

The notes are mainly from teacher linxiaobin <<MYSQL actual combat 45 speak >> Course , It is mainly used for personal learning notes , If this article infringes upon your rights , Please contact me , I will delete the infringing articles as soon as possible !

copyright notice
author[jackaroo2020],Please bring the original link to reprint, thank you.

Random recommended