由不同的索引更新解決MySQL死鎖套路
前幾篇文章介紹了用源碼的方式來(lái)調(diào)試鎖相關(guān)的信息,這里同樣用這個(gè)工具來(lái)解決一個(gè)線上實(shí)際的死鎖案例,也是我們介紹的第一個(gè)兩條 SQL 就造成死鎖的情況。因?yàn)榫€上的表結(jié)構(gòu)比較復(fù)雜,做了一些簡(jiǎn)化以后如下
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語(yǔ)句如下 # 事務(wù)1:t1 update t3 set b = '' where a = "1"; # 事務(wù)2:t2 update t3 set b = '' where b = "2";
兩條語(yǔ)句造成死鎖的情況用手動(dòng)的方式比較難復(fù)現(xiàn),我們先來(lái)分析一下加鎖的過(guò)程
第一條語(yǔ)句(通過(guò)唯一索引去更新記錄)
update t3 set b = '' where a = "1";



整理一下,加了3個(gè)X鎖,順序分別是
| 序號(hào) | 索引 | 鎖類型 |
|---|---|---|
| 1 | uk_a | X |
| 2 | PRIMARY | X |
| 3 | idx_b | X |
update t3 set b = '' where b = "2";



整理一下,加了 3 個(gè) X 鎖,順序分別是
| 序號(hào) | 索引 | 鎖類型 |
|---|---|---|
| 1 | idx_b | X |
| 2 | PRIMARY | X |
| 3 | idx_b | X |
兩條語(yǔ)句從加鎖順序看起來(lái)就已經(jīng)有構(gòu)成死鎖的條件了

手動(dòng)是比較難模擬的,寫(xiě)個(gè)代碼并發(fā)的去同時(shí)執(zhí)行那兩條 SQL 語(yǔ)句,馬上就出現(xiàn)死鎖了
------------------------ 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
事務(wù)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
事務(wù)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
事務(wù)1:想獲取普通索引 idx_b 的 X 鎖
與我們分析的完全一致,也與線上的死鎖日志一模一樣
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL日期函數(shù)與日期轉(zhuǎn)換格式化函數(shù)大全
Mysql作為一款開(kāi)元的免費(fèi)關(guān)系型數(shù)據(jù)庫(kù),用戶基礎(chǔ)非常龐大,本文列出了MYSQL常用日期函數(shù)與日期轉(zhuǎn)換格式化函數(shù)2018-03-03
MySQL數(shù)據(jù)庫(kù)基于sysbench實(shí)現(xiàn)OLTP基準(zhǔn)測(cè)試
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)基于sysbench實(shí)現(xiàn)OLTP基準(zhǔn)測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
MYSQL?Binlog恢復(fù)誤刪數(shù)據(jù)庫(kù)詳解
MySQL一旦誤刪數(shù)據(jù)庫(kù)之后恢復(fù)數(shù)據(jù)很麻煩,這里記錄一下艱辛的恢復(fù)過(guò)程,這篇文章主要給大家介紹了關(guān)于如何利用MySQL的binlog恢復(fù)誤刪數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2022-11-11
詳解MySQL實(shí)現(xiàn)主從復(fù)制過(guò)程
這篇文章主要為大家詳細(xì)介紹了MySQL主從復(fù)制的實(shí)現(xiàn)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
MySQL生成千萬(wàn)測(cè)試數(shù)據(jù)以及遇到的問(wèn)題
前兩天發(fā)現(xiàn)同事要做一個(gè)對(duì)大表進(jìn)行范圍查詢的功能,所以需要生成千萬(wàn)數(shù)據(jù)進(jìn)行性能測(cè)試,下面這篇文章主要給大家介紹了關(guān)于MySQL生成千萬(wàn)測(cè)試數(shù)據(jù)以及遇到的問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
MySQL實(shí)現(xiàn)簡(jiǎn)單的創(chuàng)建庫(kù)和創(chuàng)建表操作方法
MySQL是最常用的數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)操作中基本都是增刪改查操作,簡(jiǎn)稱CRUD,這篇文章主要給大家介紹了關(guān)于MySQL實(shí)現(xiàn)簡(jiǎn)單的創(chuàng)建庫(kù)和創(chuàng)建表操作方法的相關(guān)資料,需要的朋友可以參考下2023-11-11
mysql數(shù)據(jù)庫(kù)如何導(dǎo)入導(dǎo)出sql文件
這篇文章主要介紹了mysql數(shù)據(jù)庫(kù)如何導(dǎo)入導(dǎo)出sql文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

