InnoDB这个将近20年的"bug"修复了

简介: InnoDB这个将近20年的"bug"修复了

0. 背景信息

1. MySQL 8.0.18 以前是怎么加锁的

2. MySQL 8.0.18 之后终于变天了


0. 背景信息

最近在课程中讲到InnoDB行锁时,讲到一个知识点

InnoDB行锁规则上,有这样的一个原则:


对有唯一属性的索引(主键/唯一索引)进行范围条件加锁时,

向右遍历(假设是普通正序索引,而且不加ORDER BY … DESC约束)过程中,

会一直扫描并加next-key锁到第一个不满足条件的记录为止,

但如果是RC级别,这个next-key lock会退化成gap lock,而RR下不会退化。


简言之,就是 "锁会被扩大化",从InnoDB引擎诞生以来一直都是如此。

其实严格来说,这个算是问题或缺陷,甚至也可以认为是bu

1. MySQL 8.0.18 以前是怎么加锁的

我们看看下面的案例。

首先,确认版本、隔离级别、表结构、索引以及数据。

建议:在PC端阅读本文体验更好。

# 5.6版本
[root@yejr.run]> select version();
+------------+
| version()  |
+------------+
| 5.6.39-log |
+------------+

#隔离级别
[root@yejr.run]> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ        |
+------------------------+

#表数据
[root@yejr.run]> select * from t1;
+----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
|  0 |  0 |  0 |  0 |
|  1 |  1 |  1 |  0 |
|  3 |  3 |  3 |  0 |
|  4 |  2 |  2 |  0 |
+----+----+----+----+

#表结构&索引,c1是主键(有唯一属性),c2是辅助索引
[root@yejr.run]> show create table t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `c1` int(10) unsigned NOT NULL DEFAULT '0',
  `c2` int(10) unsigned NOT NULL DEFAULT '0',
  `c3` int(10) unsigned NOT NULL DEFAULT '0',
  `c4` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`c1`),
  KEY `c2` (`c2`)
) ENGINE=InnoDB;

下面的两个案例中,session2的请求会被阻塞

时间点 session1 sessioin2
T1 begin; begin;
T2 select * from t1 where c1<=1 for update;
T3
delete from t1 where c1=3;

被阻塞
时间点 session1 sessioin2
T1 begin; begin;
T2
delete from t1 where c1=3;
T3 select * from t1 where c1<=1 for update;

一样会被阻塞

原因在于 select * from t1 where c1<=1 for update 这个SQL中,除了对 c1<=1 的所有记录加上 LOCK_X|LOCK_ORDINARY(排他的next-key lock)之外,还需要对 c1=3 这条记录也加同样的锁

查看 information_schema 下的两个视图 innodb_locksinnodb_lock_waits 可以确认:

[root@yejr.run]> select * from INNODB_LOCKs\G

1. row **
lock_id: 2849:26:3:4 --请求的锁
lock_trx_id: 2849 --被阻塞的事务
lock_mode: X --拍他锁
lock_type: RECORD --锁类型是 LOCK_ORDINARY(即next-lock)
lock_table: `test`.`t1`
lock_index: PRIMARY
lock_space: 26
lock_page: 3
lock_rec: 4
lock_data: 3
2. row **
lock_id: 2848:26:3:4 --持有的锁
lock_trx_id: 2848 --持有锁的事务
lock_mode: X --排他锁 LOCK_X
lock_type: RECORD --锁类型是 LOCK_ORDINARY(即next-lock)
lock_table: `test`.`t1` --表
lock_index: PRIMARY --索引
lock_space: 26 --table space id
lock_page: 3 --page no
lock_rec: 4 --heap no
lock_data: 3 --被加锁的row data,即c1=3这条记录
2 rows in set (0.00 sec)

[root@yejr.run]> select * from INNODB_LOCK_waits\G
1. row **
requesting_trx_id: 2849 --请求锁的事务(被阻塞状态)
requested_lock_id: 2849:26:3:4 --请求的锁
blocking_trx_id: 2848 --持有锁的事务
blocking_lock_id: 2848:26:3:4 --持有的锁

当然了,也可以从 show engine innodb status\G 的结果中确认,这里不赘述。

2. MySQL 8.0.18 之后终于变天了

这个存在了将近20年的"bug",终于在2019.10.14发布的MySQL 8.0.18版本中被解决(修复)了,当时我居然没注意到这个release note。

InnoDB: An unnecessary next key lock was taken when performing 
a SELECT...FOR [SHARE|UPDATE] query with a WHERE condition that
specifies a range, causing one too many rows to be locked. The
most common occurrences of this issue have been addressed so
that only rows and gaps that intersect the searched range are
locked. (Bug #29508068)

简言之:就是不再需要对不必要的数据上锁啦。

再看看上面几个案例在最新的MySQL 8.0.19版本下的表现。

时间点 session1 sessioin2
T1 begin; begin;
T2 select * from t1 where c1<=1 for update;
T3
delete from t1 where c1=3;

不再被阻塞

看下加锁详情

select ENGINE_LOCK_ID,ENGINE_TRANSACTION_ID,THREAD_ID,OBJECT_NAME,INDEX_NAME,LOCK_TYPE,LOCK_MODE,LOCK_STATUS,LOCK_DATA from data_locks;
+-----------------------------------+-----------------------+-----------+-------------+------------+-----------+---------------+-------------+-----------+
| ENGINE_LOCK_ID | ENGINE_TRANSACTION_ID | THREAD_ID | OBJECT_NAME | INDEX_NAME | LOCK_TYPE | LOCK_MODE | LOCK_STATUS | LOCK_DATA |
+-----------------------------------+-----------------------+-----------+-------------+------------+-----------+---------------+-------------+-----------+
| 4868124032:1127:140327172372248 | 18983 | 351 | t1 | NULL | TABLE | IX | GRANTED | NULL |
| 4868124032:44:4:4:140327176578584 | 18983 | 351 | t1 | PRIMARY | RECORD | X,REC_NOT_GAP | GRANTED | 3 |
| 4868123176:1127:140327172370216 | 18982 | 350 | t1 | NULL | TABLE | IX | GRANTED | NULL |
| 4868123176:44:4:2:140327176573976 | 18982 | 350 | t1 | PRIMARY | RECORD | X | GRANTED | 0 |
| 4868123176:44:4:3:140327176573976 | 18982 | 350 | t1 | PRIMARY | RECORD | X | GRANTED | 1 |
+-----------------------------------+-----------------------+-----------+-------------+------------+-----------+---------------+-------------+-----------+

可以看到,select * from t1 where c1<=1 for update; 这个SQL只会对 c1=[0,1] 两条记录加上 LOCK_X|LOCK_ORDINARY 锁,不会再对 c1=3 加锁了。

这个有年头的"bug"终于被搞定了,可喜可贺。

最后,来看下关于这个"bug"的描述。当然了,公开的bug系统看不到,需要用MOS账号才可以。下面是从代码git log里的部分摘抄:

commit d1b0afd75ee669f54b70794eb6dab6c121f1f179
Author: Jakub Łopuszański <jakub.lopuszanski@oracle.com>
Date: Wed Jul 17 16:34:01 2019 +0200

Bug #29508068 UNNECESSARY NEXT-KEY LOCK TAKEN

When doing a SELECT...FOR [SHARE|UPDATE] with a WHERE condition specifying a range,
we were locking "one row too much".
This patch fixes locking behaviour in several (hopefuly) most common cases, so that
we only lock rows and gaps which intersect the searched range.

- Added MTR to demonstrate current locking policy for end of range
- Got rid of goto
- Extracted logic of determining relation between range and row to separate function
- Extracted reoccuring patterns of modifications of search_tuple so it is easier to add same for stop_tuple
- Added prebuilt->m_stop_tuple and made sure it is in sync with prebuilt->m_mysql_handler->end_range for during read_range_first() and read_range_next()
- Added row_can_be_in_range field
- Do not lock the row (just the gap) if the row is same length and after the stop_tuple
- Do not lock the row (just the gap) if the row is same length and equal to stop_tuple and strict inequality was used for end of range
- Do not lock the row (just the gap) if the row is longer than stop_tuple and its prefix is after the stop_tuple
- Do not lock the row (just the gap) if the row is longer than stop_tuple and its prefix is equal to stop_tuple and strict inequality was used for end of range
- Do not lock the row nor gap if we already saw a row same length and equal to stop_tuple in previous iteration

Reviewed-by: Pawel Olchawa <pawel.olchawa@oracle.com>
RB:22293

所以,还是赶紧升级到MySQL 8.0的最新版本吧,不光功能更强,连锁也进一步优化了。

写到这里,不禁想嘚瑟一下,加入我的「MySQL优化课」课程优势就体现出来了,一旦有重大的知识更新,总是能比别人先一步知道,图便宜买一些万年不更新的旧课,甚至是盗版视频,都是享受不到这种快感的。

有点标题党,贻笑大方了。水平有限,理解有偏差的地方,还请不吝留言指正。

全文完。

            </div>
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
4天前
|
数据采集 人工智能 安全
|
13天前
|
云安全 监控 安全
|
5天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1093 152
|
18天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1768 9
|
10天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
700 152
|
12天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
663 13
|
7天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
456 5