当前位置:网站首页>MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
2022-07-06 10:05:00 【Office template library material frog】
1、 The update statement is in MySQL How is it carried out in ?
We've analyzed it before MySQL The overall design principle of the architecture , Now for one SQL Statements are sent from our system level to MySQL in , And then one Carry out this step by step SQL The process of , Have an overall understanding .
We already know that ,MySQL The most common is InnoDB Storage engine , So today we use the execution of an update statement , Come to have a preliminary understanding Next InnoDB Architecture design of storage engine .
First, suppose we have one SQL This is the sentence :
update users set name='xxx' where id=10
Then let's think about this one first SQL How statements are executed ?
First of all, our system must have sent it to... Through a database connection MySQL On , Then it will definitely pass SQL Interface 、 Parser 、 Optimizer 、 actuator Several links , analysis SQL sentence , Generate execution plan , Then the actuator is responsible for the implementation of this plan , call InnoDB The interface of the storage engine That's ok .
So first look at the figure below , Generally, I will follow the process shown in the figure below
Today, let's explore this Architecture design in storage engine , And how to complete the execution of an update statement based on the storage engine
2、InnoDB Important memory structure : Buffer pool
InnoDB There is a very important component in memory in the storage engine , Buffer pool (Buffer Pool), There will be a lot of data cached , So as to facilitate the query in the future , In case you have data in the memory buffer pool , You don't have to check the disk , Let's look at the picture below .
So when our InnoDB Storage
When the engine wants to execute an update statement , For example, yes. “id=10” This line of data , He actually will “id=10” This line of data to see if it is in the buffer pool , If not word , Then it will be loaded directly from the disk into the buffer pool , And then an exclusive lock will be added to this row of records .
Because let's think about , After we update “id=10” When it comes to this line of data , It is definitely not allowed for others to update at the same time , So we must add An exclusive lock
As for the detailed analysis of locks , We will also have , Don't worry , Here is a preliminary understanding , Let's look at the picture below
3、undo Log files : How to rollback your updated data ?
Next step , hypothesis “id=10” This line of data name Turned out to be “zhangsan”, Now we want to update to “xxx”, Then at this time, we have to The original value to be updated “zhangsan” and “id=10” This information , Write to undo Log file .
In fact, a little bit for the database Students who know a little should know , If we execute an update statement , If he is in a business , So the thing is We can roll back the data before the service is committed , That is to update you to “xxx” The value of is rolled back to the previous “zhangsan” Go to .
So in order to consider the need to rollback data in the future , The value before your update will be written here undo Log files , Let's look at the picture below .
4、 to update buffer pool Cache data in
When we load the row of records to be updated from the disk file into the buffer pool , After locking him at the same time , It also writes the old value before the update to undo Log files after , We can officially start updating this line of records , When it's updated , First, the records in the buffer pool will be updated , At this time, this data is dirty data 了 .
The so-called updating data in the memory buffer pool , It means to put... In memory “id=10” This line of data name Change the field to “xxx”
So why is this line of data dirty ?
Because at this time, the disk “id=10” This line of data name Field or “zhangsan”, But this line of data in memory has been modified , therefore He will be called dirty data .
Let's look at the picture below , I marked the serial numbers of several steps at the same time .
5、Redo Log Buffer: In case the system goes down , How to avoid data loss ?
Then let's think about a problem , Follow the above illustration , Now the data in memory has been modified , But the data on the disk has not been modified
So what if MySQL The machine is down , It will inevitably lead to the loss of modified data in memory , What can I do about it ?
This is the time , You must write the changes made to the memory into a Redo Log Buffer In go to , This is also a buffer in memory , It's for saving discharge redo The log
So-called redo journal , Record what you did to the data , For example, yes. “id=10 This line of record has been modified name The value of the field is xxx”, this It's just a log .
Let's first look at the diagram below
This redo Logs are actually used in MySQL When it suddenly goes down , Used to restore your updated data , But we can't explain it directly yet redo How it is used , After all, now redo The log just stays in the memory buffer
Please don't be impatient , Keep looking down
6、 If the transaction has not been committed ,MySQL What to do if the machine goes down ?
Here we assume that everyone who reads the column , All the MySQL Basic SQL grammar 、 The basic concepts of transaction and index have a foundation Explain , Because every back-end engineer , Deal with databases , I must have a certain understanding of these concepts .
So we all know , In fact, in the database , Even execute one SQL sentence , In fact, it can also be an independent affair , Only when you submit a transaction after ,SQL Statement is the end of execution .
So here we all know , up to now , In fact, the transaction has not been committed yet , Now if MySQL collapse , Will inevitably lead to memory Buffer Pool in All modified data is lost , At the same time, you write Redo Log Buffer Medium redo Logs are also lost
Let's look at the picture below
So does data loss matter at this time ?
It doesn't matter , Because you have an update statement , No transaction committed , That means he didn't succeed , here MySQL Although downtime leads to data in memory It's all lost , But you will find , The data on the disk is still the same .
in other words ,“id=1” Of that line of data name The value of the field is still the old value ,“zhangsan”, So at this time, your transaction is execution failure 了 , Failed to complete the update successfully , You will receive a database exception . Then when mysql After reboot , You will find that your data has not changed .
So now if mysql Downtime , There won't be any problems .
7、 When the transaction is committed redo Log write to disk
Then we want to commit a transaction , At this point, we will put... According to certain strategies redo Log from redo log buffer Brush into the disk file .
At this point, the strategy is through innodb_flush_log_at_trx_commit To configure the , He has several options .
When the value of this parameter is 0 When , So when you submit a transaction , Will not put redo log buffer The data in the disk file , At this time, you may not be able to The transaction has been committed , result mysql It's down. , Then all the data in memory is lost .
It means that you have successfully submitted the transaction , But because of MySQL Sudden downtime , Result in data and in memory redo The logs are lost , Let's look at the picture below :
When the value of this parameter is 1 When , When you submit a transaction , You have to put redo log From memory to disk file , As long as the transaction is committed successfully , that redo log Just It must be on disk , Let's look at the picture below :
So as long as the transaction is committed successfully ,redo The log must be in the disk file , At this point, you must have one redo The log said ,“ At this time, I made a correction on which data Change , such as name Change the field to xxx 了 ”.
Then even now buffer pool The updated data in has not been refreshed to the disk , At this time, the data in the memory has been updated “name=xxx”, Then the number on the disk It is still not updated “name=zhangsan”.
Let's look at the picture below , After committing the transaction , May be in a state of .
At this time, if the transaction is submitted, it is in the state shown in the figure above , then mysql The system suddenly crashed , What will happen at this time ? Will data be lost ?
Definitely not , Because although the memory is modified to name=xxx 's data will be lost , however redo It's already said in the log , Modified the data of so and so name=xxx.
So at this time mysql After reboot , He can base on redo Log to recover previous changes , Let's look at the picture below .
Finally, let's have a look at , If innodb_flush_log_at_trx_commit The value of the parameter is 2 Well ?
What he means is , When you commit a transaction , hold redo The log is written to the disk file os cache In the cache , Instead of going directly to the disk file , can can 1 It's only in seconds os cache Write the data in the disk file .
In this mode , After you submit the transaction ,redo log May just stay in os cache In the memory cache , Did not actually enter the disk file , In case you want to The machine is down , that os cache Inside redo log Will lose , It will also make you feel committed , As a result, the data was lost , Look at the picture below .
8、 Small thinking questions : Three redo Which log disk brushing strategy to choose ?
Today, I'd like to leave you with a small question , Everyone feels that when submitting affairs , We are right. redo Which disk flushing strategy should I choose ? Each brush What are the advantages and disadvantages of disk strategy ? Why? ?
边栏推荐
- Redis distributed lock implementation redison 15 questions
- 嵌入式开发中的防御性C语言编程
- C杂讲 动态链表操作 再讲
- Several errors encountered when installing opencv
- C杂讲 文件 续讲
- [NLP] bert4vec: a sentence vector generation tool based on pre training
- Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
- Teach you how to write the first MCU program hand in hand
- CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
- MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
猜你喜欢
[CV] target detection: derivation of common terms and map evaluation indicators
17 medical registration system_ [wechat Payment]
CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
机械工程师和电气工程师方向哪个前景比较好?
15 医疗挂号系统_【预约挂号】
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
[one click] it only takes 30s to build a blog with one click - QT graphical tool
How can I take a shortcut to learn C language in college
The replay block of canoe still needs to be combined with CAPL script to make it clear
随机推荐
Pointer learning
flask运维脚本(长时间运行)
Redis distributed lock implementation redison 15 questions
简单解决phpjm加密问题 免费phpjm解密工具
Control the operation of the test module through the panel in canoe (Advanced)
在CANoe中通過Panel面板控制Test Module 運行(初級)
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
Safety notes
Nc29 search in two-dimensional array
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
History of object recognition
How does the single chip microcomputer execute the main function from power on reset?
Upload vulnerability
CAPL 脚本对.ini 配置文件的高阶操作
13 medical registration system_ [wechat login]
If a university wants to choose to study automation, what books can it read in advance?
Hero League rotation chart manual rotation
MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位
Combined search /dfs solution - leetcode daily question - number of 1020 enclaves
What should the redis cluster solution do? What are the plans?