当前位置:网站首页>How to execute an SQL statement in MySQL
How to execute an SQL statement in MySQL
2022-07-06 04:00:00 【ajdhfla】
1.mysql Basic framework :
The connector : Identity authentication and authority related ( Sign in MySQL When ).
The query cache : When executing a query statement , The cache will be queried first (MySQL 8.0 Remove after version , Because this function is not very practical ).
analyzer : If you don't hit the cache ,SQL The statement goes through the parser , The analyzer says that we should first look at your SQL What is the statement , Check your SQL Is the sentence syntax correct .
Optimizer : according to MySQL Think the best plan to carry out .
actuator : Execute statement , Then return the data from the storage engine .
Simply speaking MySQL It is mainly divided into Server Layer and storage engine layer :
Server layer : Mainly including connectors 、 The query cache 、 analyzer 、 Optimizer 、 Actuators etc. , All cross-storage engine functionality is implemented in this layer , Like stored procedures 、 trigger 、 View , Functions, etc , There is also a general log module binglog Log module .
Storage engine : Mainly responsible for data storage and reading , Using a plug-in architecture that can be replaced , Support InnoDB、MyISAM、Memory Wait for multiple storage engines , among InnoDB The engine has its own logging module redolog modular . Now the most commonly used storage engine is InnoDB, It is from MySQL 5.5.5 The version has been used as the default storage engine from the beginning .
2. analysis of sentences :
2.1 Query statement
sql It can be divided into two kinds , One is a query , One is updating ( increase , to update , Delete ). Let's analyze the query statement first , The statement is as follows :
select * from tb_student A where A.age='18' and A.name=' Zhang San ';
Combined with the above instructions , Let's analyze the execution flow of this statement :
First check whether the statement has permission , If you don't have permission , Return error message directly , If you have authority , stay MySQL8.0 Version before , The cache will be queried first , With this sql Statement for key Query in memory for results , If there is a direct cache , without , Perform the next step .
Lexical analysis by Analyzer , extract sql Key elements of the statement , For example, extracting the above statement is a query select, Extract the table name to be queried as tb_student, Need to query all columns , The query criteria are id='1'. And then judge this sql Is there any grammatical error in the statement , For example, whether the keywords are correct, etc , If the check is OK, go to the next step .
Next, the optimizer determines the execution scheme , above sql sentence , There can be two implementation options :
a. First look up the names in the student list “ Zhang San ” Of the students , Then determine if the age is 18. b. First find out the age of the students 18 Year old student , Then check the name as “ Zhang San ” Of the students .
Then the optimizer selects the best execution efficiency scheme according to its own optimization algorithm ( Optimizer thinks , Sometimes it's not the best ). After confirming the implementation plan, we are ready to start the implementation .
Verify authority , If you don't have permission, an error message will be returned , If you have permission, you will call the database engine interface , Returns the execution result of the engine .
2.2 UPDATE statement
The above is a query sql The implementation process of , So let's see how an update statement is executed ?sql The statement is as follows :
update tb_student A set A.age='19' where A.name=' Zhang San ';
This statement will basically follow the flow of the previous query , Just keep a log when updating , This will introduce the log module ,MySQL Built in log module binlog( Archive log ) , All storage engines can use , That we use a lot InnoDB The engine also comes with a log module redo log( Redo log ), We'll take InnoDB Mode to explore the execution process of this statement . The process is as follows :
First, query the data of Zhang San , If you have a cache , Cache is also used .
Then get the query statement , hold age Change it to 19, Then invoke the engine. API Interface , Write this line of data ,InnoDB The engine keeps the data in memory , Simultaneous recording redo log, here redo log Get into prepare state , Then tell the actuator , Execution completed , Ready to submit .
After the actuator receives the notice, it records binlog, Then call the engine interface , Submit redo log Is submitted status .
Update complete .
Why use two log modules :
This is because in the beginning MySQL Not with InnoDB engine ( InnoDB The engine is inserted by other companies in the form of plug-ins MySQL Of ) ,MySQL The engine is MyISAM, But we know that redo log yes InnoDB Engine specific , No other storage engine , This leads to the absence of crash-safe The ability of (crash-safe Even if the database is restarted abnormally , No records submitted before will be lost ),binlog Logs can only be used to archive .
This is not to say that only one log module is not allowed , It's just InnoDB The engine just goes through redo log To support affairs . that , Another student will ask , I use two log modules , But don't be so complicated , Why? redo log To introduce prepare Pre submission status ? Here we use the counter evidence method to explain why we should do this ?
First write redo log Submit directly , Then write binlog, Hypothesis complete redo log after , The machine hangs up ,binlog The log was not written , So after the machine restarts , This machine will go through redo log Restore data , But at this point bingog This data is not recorded , When the machine is backed up later , You lose this piece of data , At the same time, master-slave synchronization will also lose this piece of data .
First write binlog, Then write redo log, Let's say it's done binlog, The machine rebooted unexpectedly , Because there is no redo log, This record cannot be recovered by this computer , however binlog There's another record , So the same thing as above , There will be data inconsistencies .
If the redo log The two-stage submission is not the same , finish writing sth. binglog after , Then submit redo log Will prevent the above problems , So as to ensure the consistency of data . So here comes the question , Is there an extreme situation ? hypothesis redo log In pre commit state ,binglog It's finished , What happens when an abnormal restart occurs ? This depends on MySQL The processing mechanism of ,MySQL The process is as follows :
Judge redo log Is it complete , If the judgment is complete , Submit immediately .
If redo log It's just withholding but not commit state , This is the time to judge binlog Is it complete , If it's complete, submit redo log, Rollback transaction if incomplete .
This solves the problem of data consistency .
边栏推荐
- Codeforces Global Round 19
- Conditionally [jsonignore]
- Cf603e pastoral oddities [CDQ divide and conquer, revocable and search set]
- [Qt5] QT QWidget immediately appears and disappears
- 【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
- [American competition] mathematical terms
- Custom event of C (31)
- [optimization model] Monte Carlo method of optimization calculation
- User perceived monitoring experience
- Yyds dry goods inventory web components series (VII) -- life cycle of custom components
猜你喜欢
SSTI template injection explanation and real problem practice
DM8 backup set deletion
JS Vanke banner rotation chart JS special effect
WPF effect Article 191 box selection listbox
MySQL master-slave replication
ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
Simple blog system
Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
Pytoch foundation - (1) initialization of tensors
随机推荐
Proof of Stirling formula
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
Chinese brand hybrid technology: there is no best technical route, only better products
KS003基于JSP和Servlet实现的商城系统
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
Conditionally [jsonignore]
On Data Mining
记一次excel XXE漏洞
Blue style mall website footer code
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
[FPGA tutorial case 11] design and implementation of divider based on vivado core
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
Basic knowledge of binary tree, BFC, DFS
asp. Core is compatible with both JWT authentication and cookies authentication
How do we make money in agriculture, rural areas and farmers? 100% for reference
mysql从一个连续时间段的表中读取缺少数据
Thread sleep, thread sleep application scenarios
Codeforces Global Round 19
No qualifying bean of type ‘......‘ available