当前位置:网站首页>Detailed explanation of MySQL mvcc mechanism
Detailed explanation of MySQL mvcc mechanism
2022-06-26 17:56:00 【Time is light, you are safe】
1、 A brief explanation of the principle
MVCC In fact, it is implemented by using the snapshot version
MySQL There are several hidden fields at the bottom , Such as creating transactions id、 Delete transaction id
id | name | balance | Create transaction id | Delete transaction id |
1 | zhangsan | 450 | 10 | 13 |
2 | wangwu | 600 | 11 | empty |
2 | wangwu888 | 600 | 13 | empty |
ps- Be careful : At this time, a query snapshot is created , Record execution sql The largest committed transaction at this moment id( The snapshot point has committed the maximum transaction id)
For example, start the transaction now , Business id by 13,
①、 Delete first id=1 The record of
②、 And then update id=2 The record of ,
③、 Resubmit for delete .
mysql The bottom layer will record the deletion transaction of the deleted data row id,
For update operations mysql The bottom layer will add a new row of the same data and record the corresponding creation transaction id
stay id by 12 Execute query operation in the transaction of mysql The bottom layer will be provided with filter conditions , Create transaction id <= max( Current affairs id(12), The snapshot point has committed the maximum transaction id), Delete transaction id> max( Current affairs id(12), The snapshot point has committed the maximum transaction id)
Generally speaking : The data in the above table comes from .
① Start business , Inserted name by zhangsan The data of , Business id by 10, The create transaction is logged id by 10, Commit transaction
② Start business , Inserted name by wangwu The data of , Business id by 11, The create transaction is logged id by 11, Commit transaction
③ Start business , to update wangwu This record is wangwu888.MySQL In fact, the bottom layer does not directly modify the final data , Instead, a new record will be generated (PS: Generating a new record here does not mean inserting a record into our data table , It is mysql Self maintained , Every time you update, there will be a new piece of data ). New data id And the original data id It's the same , There will also be a to create transactions id, At this time, the id by 13, So the record is 13.
Then delete... From the transaction name by zhangsan The data of , At this time, delete transactions are recorded id by 13
Be careful :begin/start transaction command Not at all The starting point of a transaction , The first operation after they are executed InnoDB Statement of table , The business really starts , Will towards mysql Application affairs id,
mysql Internally, transactions are allocated strictly according to the order in which they are started id Of
2、 Instance execution
MVCC Actually, I mainly understand two sentences
①、 For delete operations ,mysql The bottom layer will record the deletion transaction of the deleted data row id, For update operations mysql The bottom layer will add a new row of the same data and record the corresponding creation transaction id
②、 Create transaction id <= max( Current affairs id(12), The snapshot point has committed the maximum transaction id), Delete transaction id> max( Current events service id(12), The snapshot point has committed the maximum transaction id)
such as , Now the data are as follows :
id | name | balance | Create transaction id | Delete transaction id |
1 | zhangsan | 450 | 10 | empty |
2 | wangwu | 600 | 11 | empty |
mysql The bottom layer will not update this data immediately , Instead, a new record will be generated (PS: Generating a new record here does not mean inserting a record into our data table , It is mysql Self maintained , Every time you update, there will be a new piece of data ). The specific data are as follows , The new record will record the current transaction id Create a transaction for id
Assuming the current session Requested transaction id by 12
id | name | balance | Create transaction id | Delete transaction id |
1 | zhangsan | 450 | 10 | empty |
2 | wangwu | 600 | 11 | empty |
2 | wangwu666 | 600 | 12 | empty |
(2) Or the current transaction , Continue to delete id by 1 The data of
mysql The underlying layer does not really delete data at this time , Instead, it records the deletion transaction of the current data id For the current transaction id, The specific data are as follows :
id | name | balance | Create transaction id | Delete transaction id |
1 | zhangsan | 450 | 10 | 12 |
2 | wangwu | 600 | 11 | empty |
2 | wangwu666 | 600 | 12 | empty |
In the same transaction , Over and over again select, Even if the values in the database have changed , Why has the inquiry result not changed ? In fact, the bottom layer reads snapshots , So the result is the same every time .
MVCC In fact, it is implemented by using the snapshot version . For a record , May be different session Modify countless times , As long as there are changes , A record will be added to the snapshot , Just the creation transaction of each record id Different .
(3) Operations before rollback , Reopen a transaction .
Execute a query statement to query data
As mentioned above begin The transaction has not really started yet , After executing the query statement, it changes back to the real start transaction ,mysql Will assign a transaction id( Assume that the currently allocated transaction id by 12)
(4) Open another session Interface , Open transaction , Query data
(5) Manually modify data in the database , And then in session1 Just now, in the business, I again begin, Check again
session1 The first transaction in the query results in the first piece of data balance Field is 350, After modifying the database value , Again begin, The queried data becomes 400.
Here we can understand the second sentence : Create transaction id <= max( Current affairs id(12), The snapshot point has committed the maximum transaction id), Delete transaction id> max( Current events service id(12), The snapshot point has committed the maximum transaction id)
The above assumption is session1 The first transaction in id yes 12
The maximum transactions committed by the snapshot point id How do you understand that ? The snapshot point is begin When the next first statement is executed , Because this time is to get the transaction id When . Now that session1 The corresponding transaction in id by 12,session2 The transaction id by 13,13 The transaction is not committed , This means that the database has committed the largest transactions id by 11( It's also a hypothetical value , As long as it is more than the current transaction id Small values are OK , because MySQL Assign transactions id There is also a sequence ), therefore ,max( Current affairs id(12), The snapshot point has committed the maximum transaction id) The value obtained is 12
because mysql The bottom layer executes the query statement , There are two conditions by default :
select * from account where create_shiwu_id <= 12( At present session The business of id) and del_shiwu_id > 12( At present session The business of id)( false SQL)
(6)、 stay session2 Delete the first data in , Then commit the transaction , Last in session1 Query in , View results
view the database , It is found that the data is indeed deleted , But in session1 Can still be found in , This is the time for business id It plays a great role ,
because mysql The bottom layer executes the query statement , There are two conditions by default :
select * from account where create_shiwu_id <= 12( At present session The business of id) and del_shiwu_id > 12( At present session The business of id)( false SQL)
session2 The business of id The assumption is 13, According to this article SQL Data cannot be queried and deleted , So you can still find the deleted data .
(7) end session1 and session2 The business of , Then restore the data in the database to the original state
session1 Zhongzhi begin, Do not query statements for the time being , The representative did not assign the affairs at that time id
session2 in begin after , Delete data , And then submit , The transaction has been allocated id, It also determines the maximum transactions committed by the current database id by session2 Of id( Assuming that 13)
So at this time , stay session1 Query again in , There will be id by 1 The data of ??
No data found after executing the statement , The query result is also deleted .
Let's understand the second sentence :
Create transaction id <= max( Current affairs id(12), The snapshot point has committed the maximum transaction id), Delete transaction id> max( Current events service id(12), The snapshot point has committed the maximum transaction id)
The above assumption is session1 The first transaction in id yes 12
The maximum transactions committed by the snapshot point id How do you understand that ? The snapshot point is begin When the next first statement is executed , Because this time is to get the transaction id When . Now that session1 The corresponding transaction in id by 12,session2 The transaction id by 13,13 The transaction for has been committed , Then we should session1 The snapshot point where the query statement is executed in , The largest committed transaction found id by 13, therefore ,max( Current affairs id(12), The snapshot point has committed the maximum transaction id) The value obtained is 13
Of the current transaction id by 12, The snapshot point has committed the maximum transaction id by 13, that max( Current affairs id(12), The snapshot point has committed the maximum transaction id) The value obtained is 13, Then the final splicing condition is :
select * from account where create_shiwu_id <= 13( At present session The business of id) and del_shiwu_id > 13( At present session The business of id)( false SQL)
So I got it session Of transactions committed in , So the data found was deleted .
边栏推荐
猜你喜欢
物联网协议的王者:MQTT
Niuke network: Design LRU cache structure design LFU cache structure
LeetCode——226. Flip binary tree (BFS)
Vue--vuerouter cache routing component
vutils. make_ A little experience of grid () in relation to black and white images
无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!
行锁与隔离级别案例分析
RSA concept explanation and tool recommendation - LMN
Here comes the hero League full skin Downloader
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!
随机推荐
行锁分析和死锁
二分查找-2
Analysis of deep security definition and encryption technology
SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
同花顺开户怎么样安全吗?怎么炒股开户
wechat_微信小程序中解决navigator进行页面跳转并传递参数问题
Regular match same character
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
数据加密标准DES安全性
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
The king of Internet of things protocol: mqtt
Prometeus 2.34.0 new features
How sparksql returns a specific day of the week by date -dayofweek function
并发之线程安全
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!
[code Capriccio - dynamic planning] t583. Deleting two strings
[buuctf.reverse] 126-130
Leetcode topic [array] -268- missing numbers
Detailed explanation of dos and attack methods
有依赖的背包问题