如何使用不同的索引更新解决MySQL死锁
这篇文章给大家分享的是有关如何使用不同的索引更新解决MySQL死锁的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联公司专注于东光网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供东光营销型网站建设,东光网站制作、东光网页设计、东光网站官网定制、微信小程序定制开发服务,打造东光网络公司原创品牌,更为您提供东光网站排名全网营销落地服务。
示例如下
CREATE TABLE `t3` ( `id` int(11) NOT NULL AUTO_INCREMENT, `a` varchar(5), `b` varchar(5), PRIMARY KEY (`id`), UNIQUE KEY `uk_a` (`a`), KEY `idx_b` (`b`) ) INSERT INTO `t3` (`id`, `a`, `b`) VALUES (1,'1','2'); # sql语句如下 # 事务1:t1 update t3 set b = '' where a = "1"; # 事务2:t2 update t3 set b = '' where b = "2";
两条语句造成死锁的情况用手动的方式比较难复现,我们先来分析一下加锁的过程
第一条语句(通过唯一索引去更新记录)
update t3 set b = '' where a = "1";
整理一下,加了3个X锁,顺序分别是
序号 | 索引 | 锁类型 |
---|---|---|
1 | uk_a | X |
2 | PRIMARY | X |
3 | idx_b | X |
第二条语句
update t3 set b = '' where b = "2";
整理一下,加了 3 个 X 锁,顺序分别是
序号 | 索引 | 锁类型 |
---|---|---|
1 | idx_b | X |
2 | PRIMARY | X |
3 | idx_b | X |
两条语句从加锁顺序看起来就已经有构成死锁的条件了
手动是比较难模拟的,写个代码并发的去同时执行那两条 SQL 语句,马上就出现死锁了
------------------------ LATEST DETECTED DEADLOCK ------------------------ 181102 12:45:05 *** (1) TRANSACTION: TRANSACTION 50AF, ACTIVE 0 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s) MySQL thread id 34, OS thread handle 0x70000d842000, query id 549 localhost 127.0.0.1 root Searching rows for update update t3 set b = '' where b = "2" *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 67 page no 3 n bits 72 index `PRIMARY` of table `d1`.`t3` trx id 50AF lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000001; asc ;; 1: len 6; hex 0000000050ae; asc P ;; 2: len 7; hex 03000001341003; asc 4 ;; 3: len 1; hex 31; asc 1;; 4: len 0; hex ; asc ;; *** (2) TRANSACTION: TRANSACTION 50AE, ACTIVE 0 sec updating or deleting mysql tables in use 1, locked 1 4 lock struct(s), heap size 1248, 3 row lock(s), undo log entries 1 MySQL thread id 35, OS thread handle 0x70000d885000, query id 548 localhost 127.0.0.1 root Updating update t3 set b = '' where a = "1" *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 67 page no 3 n bits 72 index `PRIMARY` of table `d1`.`t3` trx id 50AE lock_mode X locks rec but not gap Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000001; asc ;; 1: len 6; hex 0000000050ae; asc P ;; 2: len 7; hex 03000001341003; asc 4 ;; 3: len 1; hex 31; asc 1;; 4: len 0; hex ; asc ;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 67 page no 5 n bits 72 index `idx_b` of table `d1`.`t3` trx id 50AE lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 1; hex 32; asc 2;; 1: len 4; hex 80000001; asc ;; *** WE ROLL BACK TRANSACTION (1)
分析一下死锁日志
*** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 67 page no 3 n bits 72 index PRIMARY of table d1.t3 trx id 50AF lock_mode X locks rec but not gap waiting
事务2:想获取主键索引的 X 锁
*** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 67 page no 3 n bits 72 index PRIMARY of table d1.t3 trx id 50AE lock_mode X locks rec but not gap
事务1:持有主键索引的 X 锁
*** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 67 page no 5 n bits 72 index idx_b of table d1.t3 trx id 50AE lock_mode X locks rec but not gap waiting
事务1:想获取普通索引 idx_b 的 X 锁
与我们分析的完全一致,也与线上的死锁日志一模一样
感谢各位的阅读!关于“如何使用不同的索引更新解决MySQL死锁”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
本文名称:如何使用不同的索引更新解决MySQL死锁
标题URL:http://myzitong.com/article/ggcogh.html