两个相同查询(不同参数)的 MySQL InnoDB 死锁问题

2024-03-24

我有下表

CREATE TABLE IF NOT EXISTS `task` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `job_id` int(10) unsigned NOT NULL COMMENT 'The id of the related job',
  `server_id` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'job/task owner',
  `jobtype_id` int(10) unsigned NOT NULL DEFAULT '0',
  `node_id` int(10) unsigned NOT NULL COMMENT 'The id of the user currently executing this task',
  `status` enum('QUEUED','EXECUTING','COMPLETED','CANCELED','TERMINATED','PAUSED','FAILED') NOT NULL COMMENT 'Current status of the task',
  `last_updated` int(11) NOT NULL COMMENT 'When was the last status change of this task. Used in requeueing hung tasks',
  `data_in` blob NOT NULL COMMENT 'An input data to the task. Sets when the task is created.',
  `data_out` blob NOT NULL COMMENT 'An output data of the task. Sets upon task completion. Can be absent.',
  `speed` bigint(20) unsigned NOT NULL DEFAULT '0',
  `time_spent` int(11) NOT NULL DEFAULT '0',
  `has_data_out` tinyint(1) NOT NULL COMMENT 'Shows if the task has any output data. Serves caching purposes. Used by Summarizers to quickly find out what tasks of the job yielded data.',
  `comment` varchar(200) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `fk_task_job_id` (`job_id`),
  KEY `index_has_data_out` (`has_data_out`),
  KEY `index_last_updated` (`last_updated`),
  KEY `index_status` (`status`),
  KEY `fk_task_userid` (`node_id`),
  KEY `jobtype_id` (`jobtype_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='This table holds all subjobs - tasks' AUTO_INCREMENT=1081595 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `task`
--
ALTER TABLE `task`
  ADD CONSTRAINT `task_ibfk_5` FOREIGN KEY (`job_id`) REFERENCES `job` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `task_ibfk_7` FOREIGN KEY (`jobtype_id`) REFERENCES `jobtype` (`id`),
  ADD CONSTRAINT `task_ibfk_8` FOREIGN KEY (`node_id`) REFERENCES `node` (`id`);

以及下面的死锁问题:

------------------------
LATEST DETECTED DEADLOCK
------------------------
110831 14:23:56
*** (1) TRANSACTION:
TRANSACTION 102B4D2, ACTIVE 0 sec, OS thread id 5480
mysql tables in use 2, locked 1
LOCK WAIT 7 lock struct(s), heap size 1248, 4 row lock(s), undo log entries 3
MySQL thread id 74315, query id 2364347 192.168.1.120 usr_sl3 Sending data
select `usr_sl3`.`task`.`id` from `usr_sl3`.`task` where (`usr_sl3`.`task`.`node_id` = 103 and `usr_sl3`.`task`.`status` = 'EXECUTING') for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 38 page no 2303 n bits 576 index `index_status` of table `usr_sl3`.`task` trx id 102B4D2 lock_mode X locks rec but not gap waiting
Record lock, heap no 471 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 1; hex 02; asc  ;;
1: len 4; hex 00107dac; asc   } ;;

*** (2) TRANSACTION:
TRANSACTION 102B4D3, ACTIVE 0 sec, OS thread id 5692 starting index read, thread declared inside InnoDB 500
mysql tables in use 2, locked 1
7 lock struct(s), heap size 1248, 4 row lock(s), undo log entries 3
MySQL thread id 74354, query id 2364348 192.168.1.120 usr_sl3 Sending data
select `usr_sl3`.`task`.`id` from `usr_sl3`.`task` where (`usr_sl3`.`task`.`node_id` = 95 and `usr_sl3`.`task`.`status` = 'EXECUTING') for update
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 38 page no 2303 n bits 576 index `index_status` of table `usr_sl3`.`task` trx id 102B4D3 lock_mode X locks rec but not gap
Record lock, heap no 471 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 1; hex 02; asc  ;;
1: len 4; hex 00107dac; asc   } ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 38 page no 2303 n bits 576 index `index_status` of table `usr_sl3`.`task` trx id 102B4D3 lock_mode X locks rec but not gap waiting
Record lock, heap no 481 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 1; hex 02; asc  ;;
1: len 4; hex 00107dab; asc   } ;;

*** WE ROLL BACK TRANSACTION (2)

您能帮我理解这个僵局的机制吗?

这两个查询是从不同的线程发出的。每个线程在查询中都有自己的node_id。没有两个查询具有相同的node_id。

我怀疑,我可以通过在字段(node_id,status)上创建复合索引来解决这种情况,但这不是一个好的解决方案,我认为。我需要了解问题的本质。

同一查询上的这些死锁会定期发生,而不是一次或两次。

对受影响的查询进行 EXPLAIN 给出了有趣的结果:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  task    index_merge     index_status,fk_task_userid     index_status,fk_task_userid     1,4     NULL    1   Using intersect(index_status,fk_task_userid); Using where; Using index

MySQL版本是5.5。

此外,在死锁发生时,表不包含与受影响查询的条件匹配的行(例如 selectusr_sl3.task.id from usr_sl3.task where (usr_sl3.task.node_id= 95 和usr_sl3.task.status= '执行') 进行更新 根本不产生任何行)。

提前致谢。


该查询使用index_status索引而不是fk_task_userid(node_id上​​的索引)。这就是它用其他node_id 锁定记录的原因。

您可以对查询运行说明以查看实际锁定的记录数量(检查的行中)与需要锁定的记录数量(返回的行)

我怀疑,我可以通过在字段(node_id,status)上创建复合索引来解决这种情况,但这不是一个好的解决方案,我认为。我需要了解问题的本质。

为什么?我觉得你的索引无论如何都不是最佳的...在node_id、status上创建索引,它应该可以解决问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

两个相同查询(不同参数)的 MySQL InnoDB 死锁问题 的相关文章

随机推荐

  • 何时使用块

    我喜欢红宝石块 它们背后的想法非常非常简洁和方便 我刚刚回顾了过去一周左右的代码 基本上是我写过的每一个 ruby 函数 而且我注意到它们中没有一个返回值 我总是使用块来传回数据 而不是返回值 我什至发现自己正在考虑编写一个小状态类 这将允
  • 如何在ubuntu 15.04中安装poppler?

    Poppler 是一个基于 xpdf 3 0 代码库的 PDF 渲染库 我已经从官方网站下载了 tar xz 文件http poppler freedesktop org http poppler freedesktop org 但我不知道
  • 从另一个范围中的值定义一个范围

    我有一个 Excel 文件 其中包含已完成或未完成的任务 并在列中用 是 或 否 表示 最终 我对不同列中的数据感兴趣 但我想设置代码 以便它忽略任务已完成的那些行 到目前为止 我已经定义了包含是 否的列范围 但我不知道在此范围上运行哪个命
  • C# 中的通用 Func<> 类型

    我正在用 C 编写一个小型 Lisp 解释器 它基本上已经可以工作了 目前我正在使用一个接口来表示函数 public interface LispFunction object Apply ArrayList parameters 该接口由
  • 如何在 pygame 中有效地遮盖表面

    我按照建议掩盖 pygame 中的表面nkorth https stackoverflow com users 685933 nkorth回答问题有没有办法只在掩码中进行位块传输或更新 https stackoverflow com a 1
  • 我什么时候应该使用 stdClass,什么时候应该在 php oo 代码中使用数组?

    在工作中的大规模重构期间 我希望引入 stdClass 作为从函数返回数据的一种方式 并且我正在尝试找到非主观论据来支持我的决定 在什么情况下最好使用其中一种而不是另一种 使用 stdClass 而不是数组有什么好处 有人会说函数必须尽可能
  • 降低滚动视图中平滑滚动的速度[重复]

    这个问题在这里已经有答案了 我有一个滚动视图 我执行 smooth scroll using smoothScrollBy 一切正常 但我想更改平滑滚动的持续时间 平滑滚动发生得非常快 用户不明白发生了什么 请帮助我降低平滑滚动速度 简单的
  • 通过蓝牙打印机 Android 打印 Pdf 文件

    在我的项目中需要通过蓝牙打印机打印Pdf文件 我写了一个代码通过pdf打印 它对于文本来说很好 但我想在蓝牙打印机上打印PDF文件 我的打印文本的java代码 Override public void onCreate Bundle sav
  • 如果类包含基类的成员,编译器可以利用空基优化吗?

    Consider struct base struct child base 众所周知sizeof child 可以通过应用 1空碱基优化 然而现在 考虑一下 struct base struct child base base b 编译器
  • 在jqgrid的列中添加图像

    我想在 jqgrid 的第一列中显示一个小图像 以显示从数据库获取的所有数据 jquery tableName jqgrid colNames colModel width 25 name someValue index somevalue
  • 访问数组数组内数组的行?

    说我有 H array a array b array c a 1 2 3 4 5 6 11 22 33 44 55 66 row 1 of H 0 111 222 333 444 555 666 b 7 8 9 0 1 2 77 88 9
  • 限制 JTextField 中输入的长度不起作用

    我试图限制用户可以在文本字段中输入的字符的最大长度 但它似乎不起作用 这是代码 text2 new JTextField Enter text here 8 我做错了什么吗 如何才能使限制正常工作 您当前的代码没有设置最大长度 而是定义可见
  • BroadcastReceiver 与 WakefulBroadcastReceiver

    有人可以解释一下两者之间的确切区别是什么BroadcastReceiver https developer android com reference android content BroadcastReceiver html and W
  • WPF - 如何在任一时间仅扩展一个扩展器

    我有一个带有一组扩展器的 StackPanel 如何设置它以便在任何时候都只扩展扩展器 Cheers AW 我真的不想这样做 因为它需要将代码 C 放在窗口文件后面的类中 我试图通过使用 ViewModels 等来完全避免这种情况 理想情况
  • 如何在 Bootstrap 4 中均匀分布导航栏元素

    为网站构建引导程序导航 我在弄清楚如何最好地间隔导航栏上的导航链接元素时遇到了一些麻烦 并继续在移动设备上看起来不错 我的导航栏代码是
  • Angular (v5) 服务正在 APP_INITIALIZER 承诺解析之前构建

    我期待 Angular 等到我的loadConfig 函数在构建其他服务之前解析 但事实并非如此 应用程序模块 ts export function initializeConfig config AppConfig return gt c
  • 如何在 Rails 中订购?

    我正在开发一个小型博客引擎 有以下表格 博客和消息 博客有一个外键 last message id 因此我通过调用 blog last message 来访问博客中的最 后一条消息 我有以下代码可以使其工作 class Blog lt Ac
  • 如何调用显式实现的接口方法的基类实现?

    我试图调用在基类上实现的显式实现的接口方法 但似乎无法让它工作 我同意这个想法很难看 但我已经尝试了我能想到的所有组合 但无济于事 在这种情况下 我可以更改基类 但我想我会问这个问题来满足我的一般好奇心 有任何想法吗 example int
  • pytz时区转换性能

    我有来自数据库的超过 100 万个日期时间对象 我想将它们中的每一个转换为时区感知的日期时间对象 这是我的辅助函数 conv tz dt is python datetime object src tz and dest tz and py
  • 两个相同查询(不同参数)的 MySQL InnoDB 死锁问题

    我有下表 CREATE TABLE IF NOT EXISTS task id int 10 unsigned NOT NULL AUTO INCREMENT job id int 10 unsigned NOT NULL COMMENT