当前位置:网站首页>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? ?
边栏推荐
- History of object recognition
- Pointer learning
- 四川云教和双师模式
- Preliminary introduction to C miscellaneous lecture document
- Constants and pointers
- Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
- Listen to my advice and learn according to this embedded curriculum content and curriculum system
- Control the operation of the test module through the panel in canoe (Advanced)
- MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
- 13 医疗挂号系统_【 微信登录】
猜你喜欢
Write your own CPU Chapter 10 - learning notes
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
Cmooc Internet + education
四川云教和双师模式
Can I learn PLC at the age of 33
17 医疗挂号系统_【微信支付】
16 医疗挂号系统_【预约下单】
[CV] target detection: derivation of common terms and map evaluation indicators
Target detection -- yolov2 paper intensive reading
宝塔的安装和flask项目部署
随机推荐
Zsh configuration file
机械工程师和电气工程师方向哪个前景比较好?
华南技术栈CNN+Bilstm+Attention
Cmooc Internet + education
Nc17 longest palindrome substring
Jar runs with error no main manifest attribute
16 医疗挂号系统_【预约下单】
Delayed note learning
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
Competition vscode Configuration Guide
西南大学:胡航-关于学习行为和学习效果分析
嵌入式开发中的防御性C语言编程
Vh6501 Learning Series
C杂讲 双向循环链表
Why is 51+ assembly in college SCM class? Why not come directly to STM32
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
CAPL 脚本对.ini 配置文件的高阶操作
Teach you how to write the first MCU program hand in hand