MySQL InnoDB缺口锁定

时间:2016-07-01 08:04:50

标签: mysql indexing innodb locks

我的表格如下:

Create Table: CREATE TABLE `t` (
  `a` int(11) DEFAULT NULL,
  KEY `idx_a` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

所有数据如下:

+------+
| a    |
+------+
|   11 |
|   12 |
|   13 |
|   14 |
+------+

有两个交易同时开始: 交易1:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> delete from t where a = 11;
Query OK, 1 row affected (0.00 sec)

mysql>

交易2

mysql> start transaction;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t select 9;

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

mysql> insert into t select 15;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 2;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

问题是为什么我在事务1中删除了行a = 11,间隙(-∞,11)被锁定,这样我就无法在事务2中插入行a = 9。

这是innodb状态,请说明事务状态,事务隔离级别为REPEATABLE-READ。非常感谢你。

```
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 18446744072393214172, not started
0 lock struct(s), heap size 1080, 0 row lock(s)
---TRANSACTION 637972, ACTIVE 28 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1080, 1 row lock(s), undo log entries 1
MySQL thread id 4, OS thread handle 2804091712, query id 24 122.205.8.162 root executing
insert into t select 9
------- TRX HAS BEEN WAITING 28 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 6 page no 4 n bits 80 index idx_a of table `test`.`t` trx id 637972 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 4; hex 8000000b; asc     ;;
 1: len 6; hex 000000000414; asc       ;;

------------------
---TRANSACTION 637971, ACTIVE 32 sec
4 lock struct(s), heap size 1080, 3 row lock(s), undo log entries 1
MySQL thread id 5, OS thread handle 2802842432, query id 23 122.205.8.162 root cleaning up
```

0 个答案:

没有答案