对于使用主键进行UPDATE且在WHERE上具有相等条件的查询的MySQL死锁。为什么?

时间:2015-05-15 15:45:20

标签: mysql innodb mariadb

我有以下问题。 MySQL SHOW ENGINE INNODB状态告诉我,以下两个查询导致死锁:

  • UPDATE xf_user SET conversations_unread = '1' WHERE (user_id = 209495)
  • UPDATE xf_user SET last_activity = '1431370106' WHERE (user_id = 417599)

这怎么可能?两者都是UPDATE查询,并且在WHERE中都具有单个主键相等条件。桌子上没有约束。

SHOW CREATE TABLE xf_user:

xf_user CREATE TABLE `xf_user` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `email` varchar(120) NOT NULL,
  `gender` enum('','male','female') NOT NULL DEFAULT '' COMMENT 'Leave empty for ''unspecified''',
  `custom_title` varchar(50) NOT NULL DEFAULT '',
  `language_id` int(10) unsigned NOT NULL,
  `style_id` int(10) unsigned NOT NULL COMMENT '0 = use system default',
  `timezone` varchar(50) NOT NULL COMMENT 'Example: ''Europe/London''',
  `visible` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT 'Show browsing activity to others',
  `user_group_id` int(10) unsigned NOT NULL,
  `secondary_group_ids` varbinary(255) NOT NULL,
  `display_style_group_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'User group ID that provides user styling',
  `permission_combination_id` int(10) unsigned NOT NULL,
  `message_count` int(10) unsigned NOT NULL DEFAULT '0',
  `conversations_unread` smallint(5) unsigned NOT NULL DEFAULT '0',
  `register_date` int(10) unsigned NOT NULL DEFAULT '0',
  `last_activity` int(10) unsigned NOT NULL DEFAULT '0',
  `trophy_points` int(10) unsigned NOT NULL DEFAULT '0',
  `alerts_unread` smallint(5) unsigned NOT NULL DEFAULT '0',
  `avatar_date` int(10) unsigned NOT NULL DEFAULT '0',
  `avatar_width` smallint(5) unsigned NOT NULL DEFAULT '0',
  `avatar_height` smallint(5) unsigned NOT NULL DEFAULT '0',
  `gravatar` varchar(120) NOT NULL DEFAULT '' COMMENT 'If specified, this is an email address corresponding to the user''s ''Gravatar''',
  `user_state` enum('valid','email_confirm','email_confirm_edit','moderated','email_bounce') NOT NULL DEFAULT 'valid',
  `is_moderator` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `is_admin` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `is_banned` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `like_count` int(10) unsigned NOT NULL DEFAULT '0',
  `warning_points` int(10) unsigned NOT NULL DEFAULT '0',
  `members_page` int(10) unsigned NOT NULL DEFAULT '0',
  `stock_last_login` int(10) unsigned NOT NULL DEFAULT '0',
  `stock_current_login` int(10) unsigned NOT NULL DEFAULT '0',
  `stock_profile_validated` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `is_staff` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `is_consultant` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `fh_modified` int(11) NOT NULL DEFAULT '0',
  `activity_visible` tinyint(3) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `username` (`username`),
  KEY `email` (`email`),
  KEY `user_state` (`user_state`),
  KEY `last_activity` (`last_activity`),
  KEY `members_page` (`members_page`),
  KEY `message_count` (`message_count`),
  KEY `trophy_points` (`trophy_points`),
  KEY `like_count` (`like_count`),
  KEY `register_date` (`register_date`),
  KEY `staff_username` (`is_staff`,`username`),
  KEY `is_moderator` (`is_moderator`)
) ENGINE=InnoDB AUTO_INCREMENT=1605433 DEFAULT CHARSET=utf8

SHOW ENGINE INNODB状态摘录:

------------------------
LATEST DETECTED DEADLOCK
------------------------
2015-05-11 22:10:14 7f4240cd0700
*** (1) TRANSACTION:
TRANSACTION 3310570290, ACTIVE 1 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 21 lock struct(s), heap size 2936, 24 row lock(s), undo log entries 31
MySQL thread id 17046111, OS thread handle 0x7f42415f0700, query id 663191550 192.168.100.19 xxxxxx updating
UPDATE `xf_user` SET `conversations_unread` = '1' WHERE (user_id = 209495)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 708 page no 290 n bits 176 index `PRIMARY` of table `xxxxxx`.`xf_user` trx id 3310570290 lock_mode X locks rec but not gap waiting
*** (2) TRANSACTION:
TRANSACTION 3310570063, ACTIVE 1 sec starting index read
mysql tables in use 1, locked 1
297 lock struct(s), heap size 30248, 986 row lock(s), undo log entries 985
MySQL thread id 17046068, OS thread handle 0x7f4240cd0700, query id 663193617 192.168.100.19 xxxxxx updating
UPDATE `xf_user` SET `last_activity` = '1431370106' WHERE (user_id = 417599)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 708 page no 290 n bits 176 index `PRIMARY` of table `xxxxxx`.`xf_user` trx id 3310570063 lock_mode X locks rec but not gap
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 708 page no 1037 n bits 176 index `PRIMARY` of table `xxxxxx`.`xf_user` trx id 3310570063 lock_mode X locks rec but not gap waiting
*** WE ROLL BACK TRANSACTION (1)

MySQL版本为mysql Ver 15.1 Distrib 10.0.15-MariaDB, for Linux (x86_64) using readline 5.1

两个查询被解析为: 1 SIMPLE xf_user range PRIMARY PRIMARY 4 NULL 1 Using where

我不能在这里发布一个完整的交易,因为这不是我的代码,导致死锁,而是安装了一千个不同插件的XenForo论坛引擎。我稍后会尝试做一些倾销。

事务隔离级别为READ COMMITTED

0 个答案:

没有答案