MySQL語(yǔ)句加鎖的實(shí)現(xiàn)分析
摘要: MySQL兩條SQL語(yǔ)句鎖的分析
看一下下面的SQL語(yǔ)句加什么鎖
SLQ1:select * from t1 where id = 10; SQL2:delete from t1 where id = 10;
(1)id 是不是主鍵
(2)當(dāng)前系統(tǒng)的隔離級(jí)別是什么
(3)id列如果不是主鍵,那么id列上有索引嗎
(4)id列上如果有二級(jí)索引,那么這個(gè)索引是二級(jí)索引嗎
(5)兩個(gè)SQL的執(zhí)行計(jì)劃是什么?索引掃描還是全表掃描
實(shí)際的執(zhí)行計(jì)劃需要根據(jù)MySQL的輸出為準(zhǔn)
組合一:id列是主鍵,RC隔離級(jí)別
組合二:id列是二級(jí)唯一索引,RC隔離級(jí)別
組合三:id列是二級(jí)非唯一索引,RC隔離級(jí)別
組合四:id列沒(méi)有索引,RC隔離級(jí)別
組合五:id列是主鍵,RR隔離級(jí)別
組合六:id列是二級(jí)唯一索引,RR隔離級(jí)別
組合七:id列是二級(jí)非唯一索引,RR隔離級(jí)別
組合八:id列上沒(méi)有索引,RR隔離級(jí)別
Serializable隔離級(jí)別
在RR RC隔離級(jí)別下,SQL1:select 均不加鎖,采用的是快照讀;以下僅討論SQL2:delete操作的加鎖
Percona
組合一:id主鍵+RC
Percona
---TRANSACTION 1286310, ACTIVE 9 sec 2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1 MySQL thread id 341, OS thread handle 0x7f4d540d0700, query id 4510972 localhost root cleaning up TABLE LOCK table `test`.`t1` trx id 1286310 lock mode IX RECORD LOCKS space id 29 page no 3 n bits 80 index `PRIMARY` of table `test`.`t1` trx id 1286310 lock_mode X locks rec but not gap
MySQL
---TRANSACTION 5936, ACTIVE 171 sec 2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1 MySQL thread id 2, OS thread handle 0x7f5677201700, query id 364 localhost root TABLE LOCK table `test`.`t1` trx id 5936 lock mode IX RECORD LOCKS space id 6 page no 3 n bits 80 index `PRIMARY` of table `test`.`t1` trx id 5936 lock_mode X locks rec but not gap Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 8000000a; asc ;; 1: len 6; hex 000000001730; asc 0;; 2: len 7; hex 26000001550110; asc & U ;; 3: len 1; hex 61; asc a;;
組合二:id唯一索引+RC
在唯一索引上的更新需要兩個(gè)X鎖,一個(gè)對(duì)應(yīng)唯一索引id=10 記錄,一個(gè)對(duì)應(yīng)于聚簇索引name='d'的記錄
Percona
---TRANSACTION 1286327, ACTIVE 3 sec 3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1 MySQL thread id 344, OS thread handle 0x7f4d5404e700, query id 4510986 localhost root cleaning up TABLE LOCK table `test`.`t2` trx id 1286327 lock mode IX RECORD LOCKS space id 30 page no 4 n bits 80 index `id` of table `test`.`t2` trx id 1286327 lock_mode X locks rec but not gap RECORD LOCKS space id 30 page no 3 n bits 80 index `PRIMARY` of table `test`.`t2` trx id 1286327 lock_mode X locks rec but not gap
MySQL
---TRANSACTION 5938, ACTIVE 3 sec 3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1 MySQL thread id 2, OS thread handle 0x7f5677201700, query id 374 localhost root TABLE LOCK table `test`.`t2` trx id 5938 lock mode IX RECORD LOCKS space id 7 page no 4 n bits 80 index `id` of table `test`.`t2` trx id 5938 lock_mode X locks rec but not gap Record lock, heap no 7 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 4; hex 8000000a; asc ;; 1: len 1; hex 64; asc d;; RECORD LOCKS space id 7 page no 3 n bits 80 index `PRIMARY` of table `test`.`t2` trx id 5938 lock_mode X locks rec but not gap Record lock, heap no 7 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 64; asc d;; 1: len 6; hex 000000001732; asc 2;; 2: len 7; hex 27000001560110; asc ' V ;; 3: len 4; hex 8000000a; asc ;;
組合三:id非唯一索引+RC
ID列為普通索引,那么對(duì)應(yīng)的所有滿足SQL查詢條件的記錄,都會(huì)被加鎖;同時(shí),這些記錄在主鍵索引上的記錄,也會(huì)被加鎖
Percona
---TRANSACTION 1286339, ACTIVE 9 sec 3 lock struct(s), heap size 360, 4 row lock(s), undo log entries 2 MySQL thread id 347, OS thread handle 0x7f4b67fff700, query id 4511015 localhost root cleaning up TABLE LOCK table `test`.`t3` trx id 1286339 lock mode IX RECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1286339 lock_mode X locks rec but not gap RECORD LOCKS space id 31 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 1286339 lock_mode X locks rec but not gap
MySQL
---TRANSACTION 5940, ACTIVE 3 sec 3 lock struct(s), heap size 360, 4 row lock(s), undo log entries 2 MySQL thread id 2, OS thread handle 0x7f5677201700, query id 378 localhost root TABLE LOCK table `test`.`t3` trx id 5940 lock mode IX RECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5940 lock_mode X locks rec but not gap Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 4; hex 8000000a; asc ;; 1: len 1; hex 62; asc b;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 4; hex 8000000a; asc ;; 1: len 1; hex 64; asc d;; RECORD LOCKS space id 8 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 5940 lock_mode X locks rec but not gap Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 62; asc b;; 1: len 6; hex 000000001734; asc 4;; 2: len 7; hex 28000001570110; asc ( W ;; 3: len 4; hex 8000000a; asc ;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 64; asc d;; 1: len 6; hex 000000001734; asc 4;; 2: len 7; hex 28000001570132; asc ( W 2;; 3: len 4; hex 8000000a; asc ;;
組合四:id無(wú)索引+RC
Percona
---TRANSACTION 1286373, ACTIVE 5 sec 2 lock struct(s), heap size 360, 2 row lock(s), undo log entries 2 MySQL thread id 348, OS thread handle 0x7f4d54193700, query id 4511037 localhost root cleaning up TABLE LOCK table `test`.`t4` trx id 1286373 lock mode IX RECORD LOCKS space id 33 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 1286373 lock_mode X locks rec but not gap
MySQL
---TRANSACTION 5946, ACTIVE 2 sec 2 lock struct(s), heap size 360, 2 row lock(s), undo log entries 2 MySQL thread id 2, OS thread handle 0x7f5677201700, query id 382 localhost root TABLE LOCK table `test`.`t4` trx id 5946 lock mode IX RECORD LOCKS space id 9 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 5946 lock_mode X locks rec but not gap Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 62; asc b;; 1: len 6; hex 00000000173a; asc :;; 2: len 7; hex 2b0000015a0110; asc + Z ;; 3: len 4; hex 8000000a; asc ;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 64; asc d;; 1: len 6; hex 00000000173a; asc :;; 2: len 7; hex 2b0000015a012c; asc + Z ,;; 3: len 4; hex 8000000a; asc ;;
組合五:id主鍵+RR
參考 組合一
組合六:id唯一索引+RR
參考 組合二
組合七:id非唯一索引+RR
Percona
---TRANSACTION 1592633, ACTIVE 24 sec 4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2 MySQL thread id 794, OS thread handle 0x7f4d5404e700, query id 7801799 localhost root cleaning up Trx read view will not see trx with id >= 1592634, sees < 1592634 TABLE LOCK table `test`.`t3` trx id 1592633 lock mode IX RECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1592633 lock_mode X RECORD LOCKS space id 31 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 1592633 lock_mode X locks rec but not gap RECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1592633 lock_mode X locks gap before rec
MySQL
---TRANSACTION 5985, ACTIVE 7 sec 4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2 MySQL thread id 12, OS thread handle 0x7f56770fd700, query id 500 localhost root TABLE LOCK table `test`.`t3` trx id 5985 lock mode IX RECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5985 lock_mode X Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 4; hex 8000000a; asc ;; 1: len 1; hex 64; asc d;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 4; hex 8000000a; asc ;; 1: len 1; hex 62; asc b;; RECORD LOCKS space id 8 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 5985 lock_mode X locks rec but not gap Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 64; asc d;; 1: len 6; hex 000000001761; asc a;; 2: len 7; hex 3f0000016d0132; asc ? m 2;; 3: len 4; hex 8000000a; asc ;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 62; asc b;; 1: len 6; hex 000000001761; asc a;; 2: len 7; hex 3f0000016d0110; asc ? m ;; 3: len 4; hex 8000000a; asc ;; RECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5985 lock_mode X locks gap before rec Record lock, heap no 8 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 8000000b; asc ;; 1: len 1; hex 66; asc f;;
組合八:id無(wú)索引+RR
Percona
---TRANSACTION 1592639, ACTIVE 4 sec 2 lock struct(s), heap size 360, 7 row lock(s), undo log entries 2 MySQL thread id 794, OS thread handle 0x7f4d5404e700, query id 7801804 localhost root cleaning up TABLE LOCK table `test`.`t4` trx id 1592639 lock mode IX RECORD LOCKS space id 33 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 1592639 lock_mode X
MySQL
---TRANSACTION 6000, ACTIVE 3 sec 2 lock struct(s), heap size 360, 7 row lock(s), undo log entries 2 MySQL thread id 12, OS thread handle 0x7f56770fd700, query id 546 localhost root TABLE LOCK table `test`.`t4` trx id 6000 lock mode IX RECORD LOCKS space id 9 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 6000 lock_mode X Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 1; hex 61; asc a;; 1: len 6; hex 000000001722; asc ";; 2: len 7; hex 9e0000014e0110; asc N ;; 3: len 4; hex 8000000f; asc ;; Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 62; asc b;; 1: len 6; hex 000000001770; asc p;; 2: len 7; hex 47000001730110; asc G s ;; 3: len 4; hex 8000000a; asc ;; Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 1; hex 63; asc c;; 1: len 6; hex 000000001722; asc ";; 2: len 7; hex 9e0000014e0122; asc N ";; 3: len 4; hex 80000006; asc ;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 1; hex 64; asc d;; 1: len 6; hex 000000001770; asc p;; 2: len 7; hex 4700000173012c; asc G s ,;; 3: len 4; hex 8000000a; asc ;; Record lock, heap no 6 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 1; hex 66; asc f;; 1: len 6; hex 000000001722; asc ";; 2: len 7; hex 9e0000014e0134; asc N 4;; 3: len 4; hex 8000000b; asc ;; Record lock, heap no 7 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 2; hex 7a7a; asc zz;; 1: len 6; hex 000000001722; asc ";; 2: len 7; hex 9e0000014e013d; asc N =;; 3: len 4; hex 80000002; asc ;;
組合九:Serializable
針對(duì)前面提到的簡(jiǎn)單的SQL,最后一個(gè)情況:Serializable隔離級(jí)別。對(duì)于SQL2:delete from t1 where id = 10; 來(lái)說(shuō),Serializable隔離級(jí)別與Repeatable Read隔離級(jí)別完全一致,因此不做介紹。
Serializable隔離級(jí)別,影響的是SQL1:select * from t1 where id = 10; 這條SQL,在RC,RR隔離級(jí)別下,都是快照讀,不加鎖。但是在Serializable隔離級(jí)別,SQL1會(huì)加讀鎖,也就是說(shuō)快照讀不復(fù)存在,MVCC并發(fā)控制降級(jí)為L(zhǎng)ock-Based CC。
結(jié)論:在MySQL/InnoDB中,所謂的讀不加鎖,并不適用于所有的情況,而是隔離級(jí)別相關(guān)的。Serializable隔離級(jí)別,讀不加鎖就不再成立,所有的讀操作,都是當(dāng)前讀。
- Mysql添加聯(lián)合唯一索引及相同數(shù)據(jù)插入報(bào)錯(cuò)問(wèn)題
- mysql error 1071: 創(chuàng)建唯一索引時(shí)字段長(zhǎng)度限制的問(wèn)題
- MySQL創(chuàng)建唯一索引時(shí)報(bào)錯(cuò)Duplicate?entry?*?for?key問(wèn)題
- 如何利用MySQL添加聯(lián)合唯一索引
- MySQL 普通索引和唯一索引的區(qū)別詳解
- MySQL死鎖套路之唯一索引下批量插入順序不一致
- MySQL批量插入和唯一索引問(wèn)題的解決方法
- MySQL普通索引和唯一索引的深入講解
- 淺談MySQL next-key lock 加鎖范圍
- 一文掌握MySQL唯一索引是如何加鎖的
相關(guān)文章
在Mysql數(shù)據(jù)庫(kù)里通過(guò)存儲(chǔ)過(guò)程實(shí)現(xiàn)樹形的遍歷
關(guān)于多級(jí)別菜單欄或者權(quán)限系統(tǒng)中部門上下級(jí)的樹形遍歷,oracle中有connect by來(lái)實(shí)現(xiàn),mysql沒(méi)有這樣的便捷途徑,所以MySQL遍歷數(shù)據(jù)表是我們經(jīng)常會(huì)遇到的頭痛問(wèn)題,下面給大家介紹在Mysql數(shù)據(jù)庫(kù)里通過(guò)存儲(chǔ)過(guò)程實(shí)現(xiàn)樹形的遍歷,一起看看吧2016-11-11MySQL數(shù)據(jù)庫(kù)存儲(chǔ)引擎的應(yīng)用
存儲(chǔ)引擎是MySQL將數(shù)據(jù)存儲(chǔ)在文件系統(tǒng)中的存儲(chǔ)方式,本文主要介紹了MySQL數(shù)據(jù)庫(kù)的存儲(chǔ)引擎的應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03MySQL實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)表記錄數(shù)
這篇文章主要介紹了MySQL實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)表記錄數(shù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Mysql查詢條件判斷是否包含字符串的方法實(shí)現(xiàn)
本文主要介紹了Mysql查詢條件判斷是否包含字符串的方法實(shí)現(xiàn),主要包括like,locate,postion,instr,find_in_set這幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Mysql數(shù)據(jù)庫(kù)百萬(wàn)級(jí)數(shù)據(jù)測(cè)試索引效果
這篇文章主要為大家介紹了Mysql數(shù)據(jù)庫(kù)百萬(wàn)數(shù)據(jù)測(cè)試索引效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06MySQL中的log_bin_trust_function_creators系統(tǒng)變量
本文主要介紹了MySQL中的log_bin_trust_function_creators系統(tǒng)變量,log_bin_trust_function_creators是一個(gè)全局系統(tǒng)變量,下面就來(lái)介紹一下具體使用,感興趣的可以了解一下2024-09-09wampserver下mysql導(dǎo)入數(shù)據(jù)庫(kù)的步驟
這篇文章主要介紹了wampserver下mysql導(dǎo)入數(shù)據(jù)庫(kù)的步驟,需要的朋友可以參考下2016-08-08