Home > Database > Mysql Tutorial > body text

MySQL summarizes the MVCC principle of InnoDB

WBOY
Release: 2022-04-18 18:25:04
forward
2054 people have browsed it

This article brings you relevant knowledge about mysql, which mainly introduces issues related to the MVCC principle of InnoDB. MVCC is multi-version concurrency control, mainly to improve the concurrency of the database. Performance, let’s take a look at it, I hope it will be helpful to everyone.

MySQL summarizes the MVCC principle of InnoDB

Recommended learning: mysql video tutorial

MVCC full name is Multi-Version Concurrency Control, which is multi-version concurrency control, mainly for Improve the concurrency performance of the database. When a read or write request occurs for the same row of data, it will be locked and blocked. But MVCC uses a better way to handle read-write requests, so that no locking occurs when a read-write request conflict occurs. This read refers to the snapshot read, not the current read. The current read is a locking operation and is a pessimistic lock. So how does it achieve reading and writing without locking? What do snapshot reading and current reading mean? We will all learn this later.

MySQL can avoid phantom read problems to a large extent under the REPEATABLE READ isolation level. How does MySQL do this?

Version chain

We know that for tables using the InnoDB storage engine, its clustered index records contain two necessary hidden columns (row_id It is not necessary. The row_id column will not be included when the table we create has a primary key or a non-NULL UNIQUE key):

  • trx_id: Each time a transaction clusters a certain When the index record is modified, the transaction ID of the transaction will be assigned to the trx_id hidden column.

  • roll_pointer: Every time a clustered index record is changed, the old version will be written to the undo log, and then this hidden column is equivalent to a pointer, which can Use it to find the information before the record was modified.

In order to illustrate this problem, we create a demonstration table:

CREATE TABLE `teacher` (
  `number` int(11) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `domain` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`number`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
Copy after login

Then insert a piece of data into this table:

mysql> insert into teacher values(1, 'J', 'Java');Query OK, 1 row affected (0.01 sec)
Copy after login

The current data This is it:

mysql> select * from teacher;
+--------+------+--------+
| number | name | domain |
+--------+------+--------+
|      1 | J    | Java   |
+--------+------+--------+
1 row in set (0.00 sec)
Copy after login

Assume that the transaction ID of inserting the record is 60, then the diagram of the record at this moment is as follows:

MySQL summarizes the MVCC principle of InnoDB

Assume that the next two A transaction with transaction IDs of 80 and 120 performs an UPDATE operation on this record. The operation process is as follows:

##beginupdate teacher set name='S' where number=1;##update teacher set name='T' where number=1;##commit##update teacher set name='K' where number=1;##update teacher set name='F' where number =1;##commit
Trx80 Trx120
begin







每次对记录进行改动,都会记录一条undo日志,每条undo日志也都有一个roll_pointer属性(INSERT操作对应的undo日志没有该属性,因为该记录并没有更早的版本),可以将这些undo日志都连起来,串成一个链表,所以现在的情况就像下图一样:

MySQL summarizes the MVCC principle of InnoDB

对该记录每次更新后,都会将旧值放到一条undo日志中,就算是该记录的一个旧版本,随着更新次数的增多,所有的版本都会被roll_pointer属性连接成一个链表,我们把这个链表称之为版本链,版本链的头节点就是当前记录最新的值。另外,每个版本中还包含生成该版本时对应的事务id。于是可以利用这个记录的版本链来控制并发事务访问相同记录的行为,那么这种机制就被称之为多版本并发控制(Mulit-Version Concurrency Control MVCC)。

ReadView

对于使用READ UNCOMMITTED隔离级别的事务来说,由于可以读到未提交事务修改过的记录,所以直接读取记录的最新版本就好了。

对于使用SERIALIZABLE隔离级别的事务来说,InnoDB使用加锁的方式来访问记录。

对于使用READ COMMITTED和REPEATABLE READ隔离级别的事务来说,都必须保证读到已经提交了的事务修改过的记录,也就是说假如另一个事务已经修改了记录但是尚未提交,是不能直接读取最新版本的记录的,核心问题就是:READ COMMITTED和REPEATABLE READ隔离级别在不可重复读和幻读上的区别,这两种隔离级别关键是需要判断一下版本链中的哪个版本是当前事务可见的。

为此,InnoDB提出了一个ReadView的概念,这个ReadView中主要包含4个比较重要的内容:

  • m_ids:表示在生成ReadView时当前系统中活跃的读写事务的事务id列表。

  • min_trx_id:表示在生成ReadView时当前系统中活跃的读写事务中最小的事务id,也就是m_ids中的最小值。

  • max_trx_id:表示生成ReadView时系统中应该分配给下一个事务的id值。注意max_trx_id并不是m_ids中的最大值,事务id是递增分配的。比方说现在有id为1,2,3这三个事务,之后id为3的事务提交了。那么一个新的读事务在生成ReadView时,m_ids就包括1和2,min_trx_id的值就是1,max_trx_id的值就是4。

  • creator_trx_id:表示生成该ReadView的事务的事务id。

有了这个ReadView,这样在访问某条记录时,只需要按照下边的步骤判断记录的某个版本是否可见:

  1. 如果被访问版本的trx_id属性值与ReadView中的creator_trx_id值相同,意味着当前事务在访问它自己修改过的记录,所以该版本可以被当前事务访问。
  2. 如果被访问版本的trx_id属性值小于ReadView中的min_trx_id值,表明生成该版本的事务在当前事务生成ReadView前已经提交,所以该版本可以被当前事务访问。
  3. 如果被访问版本的trx_id属性值大于或等于ReadView中的max_trx_id值,表明生成该版本的事务在当前事务生成ReadView后才开启,所以该版本不可以被当前事务访问。
  4. 如果被访问版本的trx_id属性值在ReadView的min_trx_id和max_trx_id之间(min_trx_id
  5. 如果某个版本的数据对当前事务不可见的话,那就顺着版本链找到下一个版本的数据,继续按照上边的步骤判断可见性,依此类推,直到版本链中的最后一个版本。如果最后一个版本也不可见的话,那么就意味着该条记录对该事务完全不可见,查询结果就不包含该记录。

在MySQL中,READ COMMITTED和REPEATABLE READ隔离级别的的一个非常大的区别就是它们生成ReadView的时机不同。

我们还是以表teacher为例,假设现在表teacher中只有一条由事务id为60的事务插入的一条记录,接下来看一下READ COMMITTED和REPEATABLE READ所谓的生成ReadView的时机不同到底不同在哪里。

READ COMMITTED每次读取数据前都生成一个ReadView

假设现在系统里有两个事务id分别为80、120的事务在执行:

# Transaction 80
set session transaction isolation level read committed;
begin
update teacher set name='S' where number=1;
update teacher set name='T' where number=1;
Copy after login

此刻,表teacher中number为1的记录得到的版本链表如下所示:

MySQL summarizes the MVCC principle of InnoDB

假设现在有一个使用READ COMMITTED隔离级别的事务开始执行:

set session transaction isolation level read committed;
# 使用READ COMMITTED隔离级别的事务
begin;
# SELECE1:Transaction 80、120未提交
SELECT * FROM teacher WHERE number = 1; # 得到的列name的值为'J'
Copy after login

这个SELECE1的执行过程如下:

  • 在执行SELECT语句时会先生成一个ReadView,ReadView的m_ids列表的内容就是[80, 120],min_trx_id为80,max_trx_id为121,creator_trx_id为0。

  • 然后从版本链中挑选可见的记录,最新版本的列name的内容是’T’,该版本的trx_id值为80,在m_ids列表内,根据步骤4不符合可见性要求,根据roll_pointer跳到下一个版本。

  • 下一个版本的列name的内容是’S’,该版本的trx_id值也为80,也在m_ids列表内,根据步骤4也不符合要求,继续跳到下一个版本。

  • 下一个版本的列name的内容是’J’,该版本的trx_id值为60,小于ReadView 中的min_trx_id值,根据步骤2判断这个版本是符合要求的。

之后,我们把事务id为80的事务提交一下,然后再到事务id为120的事务中更新一下表teacher 中number为1的记录:

set session transaction isolation level read committed;
# Transaction 120
begin
update teacher set name='K' where number=1;
update teacher set name='F' where number=1;
Copy after login

此刻,表teacher 中number为1的记录的版本链就长这样:

MySQL summarizes the MVCC principle of InnoDB

然后再到刚才使用READ COMMITTED隔离级别的事务中继续查找这个number 为1的记录,如下:

# 使用READ COMMITTED隔离级别的事务
begin;
# SELECE1:Transaction 80、120未提交
SELECT * FROM teacher WHERE number = 1; # 得到的列name的值为'J'
# SELECE2:Transaction 80提交、120未提交
SELECT * FROM teacher WHERE number = 1; # 得到的列name的值为'T'
Copy after login

这个SELECE2 的执行过程如下:

  • 在执行SELECT语句时会又会单独生成一个ReadView,该ReadView的m_ids列表的内容就是[120](事务id为80的那个事务已经提交了,所以再次生成快照时就没有它了),min_trx_id为120,max_trx_id为121,creator_trx_id为0。
  • 然后从版本链中挑选可见的记录,从图中可以看出,最新版本的列name的内容是’F’,该版本的trx_id值为120,在m_ids列表内,根据步骤4不符合可见性要求,根据roll_pointer跳到下一个版本。
  • 下一个版本的列name 的内容是’K’,该版本的trx_id值为120,也在m_ids列表内,根据步骤4不符合可见性要求,根据roll_pointer跳到下一个版本。
  • 下一个版本的列name的内容是’T’,该版本的trx_id值为80,小于ReadView中的min_trx_id值120,表明生成该版本的事务在当前事务生成ReadView前已经提交,所以这个版本是符合要求的,最后返回给用户的版本就是这条列name为’‘T’'的记录。

以此类推,如果之后事务id为120的记录也提交了,再次在使用READCOMMITTED隔离级别的事务中查询表teacher中number值为1的记录时,得到的结果就是’F’了,具体流程我们就不分析了。

总结一下就是:使用READCOMMITTED隔离级别的事务在每次查询开始时都会生成一个独立的ReadView。

REPEATABLE READ —— 在第一次读取数据时生成一个ReadView

对于使用REPEATABLE READ隔离级别的事务来说,只会在第一次执行查询语句时生成一个ReadView,之后的查询就不会重复生成了。我们还是用例子看一下是什么效果。

假设现在系统里有两个事务id分别为80、120的事务在执行:

# Transaction 80
begin
update teacher set name='S' where number=1;
update teacher set name='T' where number=1;
Copy after login

此刻,表teacher中number为1的记录得到的版本链表如下所示:

MySQL summarizes the MVCC principle of InnoDB

假设现在有一个使用REPEATABLE READ隔离级别的事务开始执行:

# 使用REPEATABLE READ隔离级别的事务
begin;
# SELECE1:Transaction 80、120未提交
SELECT * FROM teacher WHERE number = 1; # 得到的列name的值为'J'
Copy after login

这个SELECE1的执行过程如下(与READ COMMITTED的过程一致):

  • 在执行SELECT语句时会先生成一个ReadView,ReadView的m_ids列表的内容就是[80, 120],min_trx_id为80,max_trx_id为121,creator_trx_id为0。

  • 然后从版本链中挑选可见的记录,最新版本的列name的内容是’T’,该版本的trx_id值为80,在m_ids列表内,根据步骤4不符合可见性要求,根据roll_pointer跳到下一个版本。

  • 下一个版本的列name的内容是’S’,该版本的trx_id值也为80,也在m_ids列表内,根据步骤4也不符合要求,继续跳到下一个版本。

  • 下一个版本的列name的内容是’J’,该版本的trx_id值为60,小于ReadView 中的min_trx_id值,根据步骤2判断这个版本是符合要求的。

之后,我们把事务id为80的事务提交一下,然后再到事务id为120的事务中更新一下表teacher 中number为1的记录:

# Transaction 80
begin
update teacher set name='K' where number=1;
update teacher set name='F' where number=1;
Copy after login

此刻,表teacher 中number为1的记录的版本链就长这样:

MySQL summarizes the MVCC principle of InnoDB

然后再到刚才使用REPEATABLE READ隔离级别的事务中继续查找这个number为1的记录,如下:

# 使用REPEATABLE READ隔离级别的事务
begin;
# SELECE1:Transaction 80、120未提交
SELECT * FROM teacher WHERE number = 1; # 得到的列name的值为'J'
# SELECE2:Transaction 80提交、120未提交
SELECT * FROM teacher WHERE number = 1; # 得到的列name的值为'J'
Copy after login

这个SELECE2的执行过程如下:

  • Because the isolation level of the current transaction is REPEATABLE READ, and ReadView has been generated before when executing SELECE1, so the previous ReadView is directly reused at this time. The content of the m_ids list of the previous ReadView is [80, 120], min_trx_id is 80, max_trx_id is 121, creator_trx_id is 0.
  • Then select visible records from the version chain. As can be seen from the figure, the content of the column name of the latest version is 'F'. The trx_id value of this version is 120. In the m_ids list, according to the steps 4 does not meet visibility requirements and jumps to the next version based on roll_pointer.
  • The content of the column name of the next version is 'K'. The trx_id value of this version is 120, which is also in the m_ids list. According to step 4, it does not meet the visibility requirements and jumps to the next version according to the roll_pointer.
  • The content of the column name of the next version is 'T'. The trx_id value of this version is 80, which is also in the m_ids list. According to step 4, it does not meet the visibility requirements and jumps to the next version according to the roll_pointer.
  • The content of the column name of the next version is 'S'. The trx_id value of this version is 80, which is also in the m_ids list. According to step 4, it does not meet the visibility requirements and jumps to the next version according to the roll_pointer.
  • The content of the column name of the next version is 'J'. The trx_id value of this version is 60, which is less than the min_trx_id value 80 in ReadView, indicating that the transaction that generated this version has been submitted before the current transaction generates ReadView. So this version meets the requirements, and the version finally returned to the user is the record with the column name ''J''.

That is to say, the results obtained by the two SELECT queries are repeated, and the recorded column values ​​​​are all '''J''''. This is the meaning of repeatable reading.

If we submit the record with transaction ID 120 later, and then continue to search for the record with number 1 in the transaction that just used REPEATABLE READ isolation level, the result will still be 'J', specifically You can analyze the execution process yourself.

Phantom reading phenomenon and phantom reading solution under MVCC

We already know that MVCC can solve the non-repeatable reading problem under the REPEATABLE READ isolation level, so phantom reading Woolen cloth? How is MVCC solved? Phantom reading is when a transaction reads records multiple times according to the same conditions. The last read reads a record that has not been read before, and this record comes from a new record added by another transaction.

We can think about it, transaction T1 under the REPEATABLE READ isolation level first reads multiple records based on a certain search condition, then transaction T2 inserts a record that meets the corresponding search condition and submits it, and then transaction T1 Then execute the query based on the same search criteria. What will be the result? According to the comparison rules in ReadView:

Regardless of whether transaction T2 is opened before transaction T1, transaction T1 cannot see the submission of T2. Please analyze it yourself according to the version chain, ReadView and visibility judgment rules introduced above.

However, MVCC in InnoDB under the REPEATABLE READ isolation level can avoid phantom reading to a large extent, rather than completely prohibiting phantom reading. What's going on? Let’s look at the following situation:

##commit; update teacher set domain='MQ' where number=30;select * from teacher where number = 30; There is data

Well, what's going on? Transaction T1 obviously has a phantom read phenomenon. Under the REPEATABLE READ isolation level, T1 generates a ReadView when executing a normal SELECT statement for the first time, and then T2 inserts a new record into the teacher table and submits it. ReadView cannot prevent T1 from executing the UPDATE or DELETE statement to change the newly inserted record (since T2 has already submitted, changing the record will not cause blocking), but in this way, the value of the trx_id hidden column of this new record will be It becomes the transaction id of T1. After that, T1 can see this record when it uses an ordinary SELECT statement to query this record, and can return this record to the client. Because of the existence of this special phenomenon, we can also think that MVCC cannot completely prohibit phantom reading.

MVCC Summary

We can see from the above description that the so-called MVCC (Multi-Version ConcurrencyControl, multi-version concurrency control) refers to the use of READ COMMITTD and REPEATABLE READ. This isolation level transaction accesses the recorded version chain when performing a normal SELECT operation. This allows read-write and write-read operations of different transactions to be executed concurrently, thus improving system performance.

A big difference between the two isolation levels of READ COMMITTD and REPEATABLE READ is that the timing of generating ReadView is different. READ COMMITTD will generate a ReadView before each ordinary SELECT operation, while REPEATABLE READ will only generate a ReadView after the normal SELECT operation. Just generate a ReadView before performing a normal SELECT operation, and reuse this ReadView for subsequent query operations, thus basically avoiding the phenomenon of phantom reading.

We said before that executing a DELETE statement or an UPDATE statement that updates the primary key will not immediately completely delete the corresponding record from the page. Instead, it will perform a so-called delete mark operation, which is equivalent to just marking the record with a mark. Delete the flag bit, which mainly serves MVCC. In addition, the so-called MVCC only takes effect when we perform ordinary SEELCT queries. All SELECT statements we have seen so far are ordinary queries. As for what is an extraordinary query, we will talk about it later.

Recommended learning: mysql video tutorial

T1 T2
begin;
select * from teacher where number=30; No data begin;

insert into teacher values(30, 'X', 'Java');



The above is the detailed content of MySQL summarizes the MVCC principle of InnoDB. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template