剔除无用数据的表结构
CREATE TABLE `pyjy_vd_member_feature_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL DEFAULT '0',
`feature_id` int(11) NOT NULL DEFAULT '0',
`state` tinyint(2) NOT NULL DEFAULT '1' COMMENT '1 尚未提交审核 2 正在审核 3 审核通过 4 审核拒绝',
`content` varchar(300) CHARACTER SET utf8 NOT NULL DEFAULT '',
`main_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `member_id_index` (`member_id`),
KEY `feature_id_index` (`feature_id`),
KEY `member_feature_content_state_idx` (`state`),
KEY `member_feature_content_main_id_idx` (`main_id`),
KEY `index_0` (`type`,`show_state`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=228734 DEFAULT CHARSET=utf8mb4 COMMENT='用户特质内容';
发生死锁的两条 sql
UPDATE `pyjy_vd_member_feature_content` SET `view_count` = `view_count` + 85 WHERE `feature_id` = '95' AND `member_id` = 549872 AND `main_id` = '140359' AND `state` = 3
UPDATE `pyjy_vd_member_feature_content` SET `view_count` = `view_count` + 88 WHERE `feature_id` = '95' AND `member_id` = 363520 AND `main_id` = '118167' AND `state` = 3
在 rc 模式下,不可重复读,所以加的锁是记录锁
这个是事务 1 的日志
RECORD LOCKS space id 377 page no 1530 n bits 1272 index feature_id_index of table `vdsns`.`pyjy_vd_member_feature_content` trx id 204978888 lock_mode X locks rec but not gap waiting
Record lock, heap no 264 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;; //普通索引=》 feature_id
1: len 4; hex 8002e0ff; asc ;; //主键 ID
这个是事务二的日志
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 377 page no 1530 n bits 1272 index feature_id_index of table `vdsns`.`pyjy_vd_member_feature_content` trx id 204978887 lock_mode X locks rec but not gap
Record lock, heap no 264 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002e0ff; asc ;;
Record lock, heap no 265 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002e103; asc ;;
Record lock, heap no 267 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002e108; asc ;;
Record lock, heap no 268 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002e109; asc ;;
Record lock, heap no 286 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002e165; asc e;;
Record lock, heap no 287 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002e166; asc f;;
Record lock, heap no 515 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 8002ebf3; asc ;;
Record lock, heap no 1084 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 8000005f; asc _;;
1: len 4; hex 80031e5e; asc ^;;
现在知道死锁是 feature_id 这个字段造成的。 但是这里边是怎么执行的,还不理解。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.