锁定数据:MySQL5.7.11中的supremum伪记录,带有REPEATABLE-READ

时间:2016-03-31 07:28:50

标签: mysql database-locking

MySQL5.7.11,tx_isolation是REPEATABLE-READ;

表格如下:

CREATE TABLE a (
  id int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

insert into a values(1);
在session1中

,执行如下:

begin;
select * from a where id=2 for update;

然后在session2中执行:

begin;
insert into a values(3);

session2被阻止,我认为session2因为GAP锁定而被阻止, 但是在information_schema.innodb_lock中,它显示了supremum伪记录和RECORD Lock;

*************************** 1. row ***************************
    lock_id: 234076:115:3:1
lock_trx_id: 234076
  lock_mode: X
  lock_type: RECORD
 lock_table: `test`.`a`
 lock_index: PRIMARY
 lock_space: 115
  lock_page: 3
   lock_rec: 1
  lock_data: supremum pseudo-record
*************************** 2. row ***************************
    lock_id: 234075:115:3:1
lock_trx_id: 234075
  lock_mode: X
  lock_type: RECORD
 lock_table: `test`.`a`
 lock_index: PRIMARY
 lock_space: 115
  lock_page: 3
   lock_rec: 1
  lock_data: supremum pseudo-record
2 rows in set (0.00 sec)

MySQL_Doc将其描述如下:

For the last interval, the next-key lock locks the gap above the largest value in the index and the
“supremum” pseudo-record having a value higher than any value actually in the index. The supremum
is not a real index record, so, in effect, this next-key lock locks only the gap following the largest index
value.

为什么Record Lock和lock_data是supremum伪记录?

1 个答案:

答案 0 :(得分:0)

StackOverflow answer非常清楚地解释了这一点。

基本上,如果没有可用的下一个键,比如当您锁定表格末尾的范围时,MySQL将使用pseudo-record,因为它不知道范围结束的地方。

在这种情况下,锁将阻止您添加超出SQL查询WHERE子句中使用的谓词锁边界的条目。