在mysql数据库中,间隙锁(gap locks)和临键锁(next-key locks)是实现行级锁的一种方式,它们主要用于支持事务的隔离级别,尤其是在可重复读(repeatable read)和串行化(serializable)隔离级别下。间隙锁和临键锁共同确保了事务的隔离性和一致性,防止了幻读(phantom reads)的发生。
间隙锁(gap locks)
间隙锁锁定的是一个范围,但不包括记录本身。例如,如果事务a在范围(10, 20)上加了间隙锁,那么其他事务就不能在这个范围内插入新的记录。
临键锁(next-key locks)
临键锁是间隙锁和记录锁的结合,锁定一个范围并包括该范围内的最高记录。在mysql的innodb存储引擎中,默认情况下,所有行级锁都是临键锁。这意味着如果一个事务对某个范围内的记录加了临键锁,那么其他事务既不能在这个范围内插入新记录,也不能修改或删除这个范围内的任何现有记录。
版本:mysql-8.4.9
表结构:
create table `employees2` ( `id` int not null auto_increment, `name` varchar(24) not null default '' comment '姓名', `age` int not null default '0' comment '年龄', `position` varchar(20) not null default '' comment '职位', `hire_time` timestamp not null default current_timestamp comment '入职时间', primary key (`id`), key `age_index` (`age`) ) engine=innodb auto_increment=200104 default charset=utf8mb3 comment='员工记录表';
表数据:
mysql> select * from employees2; +----+------+-----+----------+---------------------+ | id | name | age | position | hire_time | +----+------+-----+----------+---------------------+ | 5 | a | 5 | dev | 2026-08-07 08:57:09 | | 10 | b | 10 | test | 2026-08-07 08:57:19 | | 15 | c | 15 | prod | 2026-08-07 08:57:38 | | 20 | d | 20 | prod2 | 2026-08-07 08:57:52 | +----+------+-----+----------+---------------------+ 4 rows in set (0.00 sec)
索引:age_index 为普通索引 , rr 级别;
事务1: 开启事务,查询age>10 and age < 15 并加上排他锁。此时事务1会锁住10到15 区间。包括10,不包括15。[10,15)
begin; select * from employees2 where age >10 and age <15 for update;
事务2: age = 9 时,直接提交成功,无需等待事务1完成
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (21,'a',9,'aa'); query ok, 1 row affected (0.00 sec) mysql> commit; query ok, 0 rows affected (0.00 sec)
事务2: age = 10 ,必须等待事务1 提交后,才能提交。
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (22,'a',10,'aa'); error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql>
执行结果:

正常执行:
t2 执行 insert into employees2 (id,name,age,position) values (22,‘a’,10,‘aa’); 后,t1 commit 提交后,t2获取到锁后正常提交。

结果:此范围查询间隙锁范围包括10;
事务1继续:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> select * from employees2 where age > 10 and age < 15 for update; empty set (0.00 sec) mysql>
事务2: age = 15 是正常添加,age=15 不在事务1间隙锁范围
mysql> begin;
query ok, 0 rows affected (0.00 sec)
mysql> insert into employees2 (id,name,age,position) values (23,'a',15,'aa');
query ok, 1 row affected (0.00 sec)
mysql> commit
-> ;
query ok, 0 rows affected (0.00 sec)结果

事务2 继续: 插入age = 14 时,在事务1间隙锁范围内,无法插入。需要等待事务1 执行结束。
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (25,'a',14,'aa'); error 1205 (hy000): lock wait timeout exceeded; try restarting transaction

结果:此范围查询间隙锁范围不包括15 ;
所以:
select * from employees2 where age > 10 and age < 15 for update;
age普通索引,间隙锁范围是:包括10 ,但是不包括15 ,区间为 [10,15)
场景2 : select * from employees2 where age >= 10 and age <= 15 for update;
数据恢复
mysql> select * from employees2; +----+------+-----+----------+---------------------+ | id | name | age | position | hire_time | +----+------+-----+----------+---------------------+ | 5 | a | 5 | dev | 2026-08-07 08:57:09 | | 10 | b | 10 | test | 2026-08-07 08:57:19 | | 15 | c | 15 | prod | 2026-08-07 08:57:38 | | 20 | d | 20 | prod2 | 2026-08-07 08:57:52 | +----+------+-----+----------+---------------------+ 4 rows in set (0.00 sec)
事务1:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> select * from employees2 where age >= 10 and age <= 15 for update; +----+------+-----+----------+---------------------+ | id | name | age | position | hire_time | +----+------+-----+----------+---------------------+ | 10 | b | 10 | test | 2026-08-07 08:57:19 | | 15 | c | 15 | prod | 2026-08-07 08:57:38 | +----+------+-----+----------+---------------------+ 2 rows in set (0.00 sec) mysql>
事务2: 插入 age = 4 没有问题,插入age=5 时,无法插入。原因事务1 的间隙锁 包含 age=5
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (21,'a',4,'aa'); query ok, 1 row affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (22,'a',5,'aa'); error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql>

正常情况:事务1 commit后,事务2立即插入age=5成功。


事务2继续: 事务2插入age=20 正常插入,插入age=19 时,无法插入。原因事务1 的间隙锁 包含 age=19**
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (23,'a',20,'aa'); query ok, 1 row affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (24,'a',19,'aa'); error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql>

age >= 10 and age <= 15 for update;
携带=时,普通索引 的区间 age 在 [5,20) ,包含5,不包含20.
场景3:select * from employees2 where age between 10 and 15 for update;;
与 age >= 10 and age <= 15 for update; 一致, 事务1间隙锁区间在 [5,20)

使用主键索引:
sql:select * from employees2 where id >= 10 and id <= 15 for update;
区间范围为:[10,15]
事务1:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> select * from employees2 where id >= 10 and id <= 15 for update; +----+------+-----+----------+---------------------+ | id | name | age | position | hire_time | +----+------+-----+----------+---------------------+ | 10 | b | 10 | test | 2026-08-07 08:57:19 | | 15 | c | 15 | prod | 2026-08-07 08:57:38 | +----+------+-----+----------+---------------------+ 2 rows in set (0.00 sec) mysql>
事务2:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (9,'a',5,'aa'); query ok, 1 row affected (0.00 sec) mysql> insert into employees2 (id,name,age,position) values (16,'a',5,'aa'); query ok, 1 row affected (0.00 sec) mysql> update employees2 set name = 'bb' where id = 10; error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql> update employees2 set name = 'bb' where id = 15; error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql>
sql: select * from employees2 where id > 10 and id < 15 for update;
事务1间隙锁区间范围为:(10,15)
事务1:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> select * from employees2 where id > 10 and id < 15 for update; empty set (0.00 sec) mysql>
事务2:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> mysql> update employees2 set name = 'bb' where id = 10; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mysql> update employees2 set name = 'bb' where id = 15; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mysql> insert into employees2 (id,name,age,position) values (11,'a',1,'aa'); error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql> insert into employees2 (id,name,age,position) values (14,'a',1,'aa'); error 1205 (hy000): lock wait timeout exceeded; try restarting transaction mysql>
sql:select * from employees2 where id between 10 and 15 for update;
事务1 间隙锁区间范围为:[10,15]
事务1:
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> select * from employees2 where id between 10 and 15 for update; +----+------+-----+----------+---------------------+ | id | name | age | position | hire_time | +----+------+-----+----------+---------------------+ | 10 | b | 10 | test | 2026-08-07 08:57:19 | | 15 | c | 15 | prod | 2026-08-07 08:57:38 | +----+------+-----+----------+---------------------+ 2 rows in set (0.00 sec) mysql>
事务2:
mysql> begin
-> ;
query ok, 0 rows affected (0.00 sec)
mysql> insert into employees2 (id,name,age,position) values (9,'a',5,'aa');
query ok, 1 row affected (0.00 sec)
mysql> insert into employees2 (id,name,age,position) values (16,'a',5,'aa');
query ok, 1 row affected (0.00 sec)
mysql> update employees2 set name = 'bb' where id = 10;
error 1205 (hy000): lock wait timeout exceeded; try restarting transaction
mysql> update employees2 set name = 'bb' where id = 15;
error 1205 (hy000): lock wait timeout exceeded; try restarting transaction
mysql>到此这篇关于mysql间隙锁与临键锁测试(最新推荐)的文章就介绍到这了,更多相关mysql间隙锁与临键锁内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论