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

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: 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;
相关文章
|
存储 缓存 网络协议
DPDK入门(环境搭建以及小demo)
DPDK入门(环境搭建以及小demo)
1459 0
|
SQL NoSQL AliSQL
MySQL RENAME hang问题分析与修复
问题现象:alter 过程 rename 文件一直失败,导致 crash。[Warning] InnoDB: Cannot rename file ./tradesupplymember/memberpropertyinfo_0024.ibd (space id 78102), retried 1000 times. There are either pending IOs or flushes 
787 0
MySQL RENAME hang问题分析与修复
|
存储 NoSQL 关系型数据库
|
Java 数据库 Spring
springBoot 整合 hikari
springBoot 整合 hikari
763 6
|
canal 缓存 关系型数据库
Canal binlog 日志 Dump 流程分析
Canal binlog 日志 Dump 流程分析
Canal binlog 日志 Dump 流程分析
|
SQL 存储 缓存
全网最清楚的:MySQL的insert buffer和change buffer 串讲
全网最清楚的:MySQL的insert buffer和change buffer 串讲
782 0
|
SQL 关系型数据库 MySQL
MySQL 并发更新唯一键和插入数据导致死锁
一 前言死锁,其实是一个很有意思也很有挑战的技术问题,大概每个DBA和部分开发同学都会在工作过程中遇见 。关于死锁我会持续写一个系列的案例分析,希望能够对想了解死锁的朋友有所帮助。二 案例分析2.1 业务场景业务开发同学想同步数据,他们的逻辑是通过update 更新操作,如果更新记录返回的affec...
928 0
|
SQL 安全 UED
独家揭秘阿里云SQL Server AlwaysOn集群版重大突破
## 缘起 早在2015年的时候,随着阿里云业务突飞猛进的发展,SQLServer业务也积累了大批忠实客户,其中一些体量较大的客户在类似大促的业务高峰时RDS的单机规格(规格是按照 内存*CPU*IOPS 一定比例分配,根据底层资源不同都会有各自上限)已经不能满足用户的业务需求,在我们看来也需要做.
5006 0

热门文章

最新文章