InnoDB事务锁之行锁-insert唯一二级索引重复键加锁案例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了InnoDB事务锁之行锁-insert唯一二级索引重复键加锁案例相关的知识,希望对你有一定的参考价值。
1、表结构
CREATE TABLE `t3` (
`id` int(11) NOT NULL,
`id2` int(11) DEFAULT NULL,
`id3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id2` (`id2`,`id3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、数据
mysql> select *from t3;
+----+------+------+
| id | id2 | id3 |
+----+------+------+
| 6 | 1 | 1 |
| 8 | 1 | 2 |
| 10 | 1 | 3 |
| 7 | 4 | 4 |
| 1 | 6 | 6 |
| 4 | 7 | 7 |
+----+------+------+
6 rows in set (0.00 sec)
3、插入数据
mysql> insert into t3 values(11,1,3);
ERROR 1062 (23000): Duplicate entry ‘1-3‘ for key ‘id2‘
4、show engine innodb status\G
---TRANSACTION 17751, ACTIVE 17 sec
2 lock struct(s), heap size 320, 1 row lock(s)
MySQL thread id 10, OS thread handle 0xa32ffb90, query id 193 localhost root init
show engine innodb status
TABLE LOCK table `yzs`.`t3` trx id 17751 lock mode IX
RECORD LOCKS space id 30 page no 4 n bits 80 index `id2` of table `yzs`.`t3` trx id 17751 lock mode S
Record lock, heap no 7 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 4; hex 80000003; asc ;;
2: len 4; hex 8000000a; asc ;;
5、加锁原理图
6、总结
唯一二级索引重复键检查中,如果遇到重复key,那么会对重复的key加S类型的next-key锁。注意:这里不论是什么隔离级别加的都是next-key锁
以上是关于InnoDB事务锁之行锁-insert唯一二级索引重复键加锁案例的主要内容,如果未能解决你的问题,请参考以下文章