6

mysql 在 rc 模式下的锁机制

 2 years ago
source link: https://www.v2ex.com/t/784567
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  MySQL

mysql 在 rc 模式下的锁机制

  awanganddong · 7 小时 35 分钟前 · 259 次点击
剔除无用数据的表结构
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 这个字段造成的。 但是这里边是怎么执行的,还不理解。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK