MYSQL RC模式insert update 可能死鎖的情況
涉及的語句為
RC模式下
update根據主鍵更新和insert
其實這樣的問題在RC模式下,要麼是簡單update問題,要麼是insert造成的主鍵和唯一鍵檢查唯一性時出現問題。
下面以主鍵問題為列子進行分析一下可能出現的情況。
update where條件更新為主鍵,鎖結構出現在單行主鍵上,輔助索引包含隱含鎖結構,當前讀RC非唯一索引模式沒有GAP鎖,
insert 插入印象鎖,主鍵和輔助索引上會出現隱含鎖結構,
但是在RC模式下沒有GAP所以插入印象鎖一般不會成為問題
表結構:
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testlll | CREATE TABLE `testlll` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=latin1 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
情況1
insert
update
TX1: TX2:
insert into testlll(name) values('gaopeng');
insert into testlll(name) values('gaopeng');
update testlll set name='gaopeng1' where id=25;(堵塞)
update testlll set name='gaopeng1' where id=24;(堵塞)
死鎖
鎖結構:
情況2
update
update
TX1: TX2:
update testlll set name='gaopeng1' where id=22;
update testlll set name='gaopeng1' where id=25;
update testlll set name='gaopeng1' where id=25;(堵塞)
update testlll set name='gaopeng1' where id=22;(堵塞)
死鎖
這種情況比較簡單不列印出鎖結構
情況3
insert
insert
TX1: TX2:
insert into testlll values(26,'gaopeng');
insert into testlll values(27,'gaopeng');
nsert into testlll values(27,'gaopeng');(堵塞)
insert into testlll values(26,'gaopeng');(堵塞)
死鎖
鎖結構:
RC模式下
update根據主鍵更新和insert
其實這樣的問題在RC模式下,要麼是簡單update問題,要麼是insert造成的主鍵和唯一鍵檢查唯一性時出現問題。
下面以主鍵問題為列子進行分析一下可能出現的情況。
update where條件更新為主鍵,鎖結構出現在單行主鍵上,輔助索引包含隱含鎖結構,當前讀RC非唯一索引模式沒有GAP鎖,
insert 插入印象鎖,主鍵和輔助索引上會出現隱含鎖結構,
但是在RC模式下沒有GAP所以插入印象鎖一般不會成為問題
表結構:
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testlll | CREATE TABLE `testlll` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=latin1 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
情況1
insert
update
TX1: TX2:
insert into testlll(name) values('gaopeng');
insert into testlll(name) values('gaopeng');
update testlll set name='gaopeng1' where id=25;(堵塞)
update testlll set name='gaopeng1' where id=24;(堵塞)
死鎖
鎖結構:
點選(此處)摺疊或開啟
-
---TRANSACTION 322809, ACTIVE 30 sec starting index read
-
mysql tables in use 1, locked 1
-
3 lock struct(s), heap size 1160, 2 row lock(s), undo log entries 1
-
MySQL thread id 3, OS thread handle 140734663714560, query id 409 localhost root updating
-
update testlll set name='gaopeng1' where id=24
-
---lock strcut(1):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
TABLE LOCK table `test`.`testlll` trx id 322809 lock mode IX
-
---lock strcut(2):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 322809 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP)
-
Record lock, heap no 25 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 80000019; asc ;;
-
1: len 6; hex 00000004ecf9; asc ;;
-
2: len 7; hex f0000001f90110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
---lock strcut(3):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 322809 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP) waiting(LOCK_WAIT)
-
Record lock, heap no 20 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 80000018; asc ;;
-
1: len 6; hex 00000004ecf8; asc ;;
-
2: len 7; hex ef000001f80110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
---TRANSACTION 322808, ACTIVE 43 sec starting index read
-
mysql tables in use 1, locked 1
-
LOCK WAIT 3 lock struct(s), heap size 1160, 2 row lock(s), undo log entries 1
-
MySQL thread id 2, OS thread handle 140734663980800, query id 408 localhost root updating
-
update testlll set name='gaopeng1' where id=25
-
------- TRX HAS BEEN WAITING 5 SEC FOR THIS LOCK TO BE GRANTED:
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 322808 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP) waiting(LOCK_WAIT)
-
Record lock, heap no 25 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 80000019; asc ;;
-
1: len 6; hex 00000004ecf9; asc ;;
-
2: len 7; hex f0000001f90110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
------------------
-
---lock strcut(1):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
TABLE LOCK table `test`.`testlll` trx id 322808 lock mode IX
-
---lock strcut(2):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 322808 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP) waiting(LOCK_WAIT)
-
Record lock, heap no 25 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 80000019; asc ;;
-
1: len 6; hex 00000004ecf9; asc ;;
-
2: len 7; hex f0000001f90110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
---lock strcut(3):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 322808 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP)
-
Record lock, heap no 20 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 80000018; asc ;;
-
1: len 6; hex 00000004ecf8; asc ;;
-
2: len 7; hex ef000001f80110; asc ;;
- 3: len 7; hex 67616f70656e67; asc gaopeng;;
update
update
TX1: TX2:
update testlll set name='gaopeng1' where id=22;
update testlll set name='gaopeng1' where id=25;
update testlll set name='gaopeng1' where id=25;(堵塞)
update testlll set name='gaopeng1' where id=22;(堵塞)
死鎖
這種情況比較簡單不列印出鎖結構
情況3
insert
insert
TX1: TX2:
insert into testlll values(26,'gaopeng');
insert into testlll values(27,'gaopeng');
nsert into testlll values(27,'gaopeng');(堵塞)
insert into testlll values(26,'gaopeng');(堵塞)
死鎖
鎖結構:
點選(此處)摺疊或開啟
-
---TRANSACTION 422212176315800, not started
-
0 lock struct(s), heap size 1160, 0 row lock(s)
-
---TRANSACTION 323284, ACTIVE 10 sec inserting
-
mysql tables in use 1, locked 1
-
3 lock struct(s), heap size 1160, 2 row lock(s), undo log entries 1
-
MySQL thread id 2, OS thread handle 140734663980800, query id 369 localhost root update
-
insert into testlll values(26,'gaopeng')
-
---lock strcut(1):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
TABLE LOCK table `test`.`testlll` trx id 323284 lock mode IX
-
---lock strcut(2):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 323284 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP)
-
Record lock, heap no 27 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 8000001b; asc ;;
-
1: len 6; hex 00000004eed4; asc ;;
-
2: len 7; hex d3000002a10110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
-
---lock strcut(3):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 323284 lock mode S(LOCK_S) locks rec but not gap(LOCK_REC_NOT_GAP) waiting(LOCK_WAIT)
-
Record lock, heap no 26 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 8000001a; asc ;;
-
1: len 6; hex 00000004eed3; asc ;;
-
2: len 7; hex d2000002330110; asc 3 ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
-
---TRANSACTION 323283, ACTIVE 14 sec inserting
-
mysql tables in use 1, locked 1
-
LOCK WAIT 3 lock struct(s), heap size 1160, 2 row lock(s), undo log entries 1
-
MySQL thread id 3, OS thread handle 140734663714560, query id 368 localhost root update
-
insert into testlll values(27,'gaopeng')
-
------- TRX HAS BEEN WAITING 5 SEC FOR THIS LOCK TO BE GRANTED:
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 323283 lock mode S(LOCK_S) locks rec but not gap(LOCK_REC_NOT_GAP) waiting(LOCK_WAIT)
-
Record lock, heap no 27 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 8000001b; asc ;;
-
1: len 6; hex 00000004eed4; asc ;;
-
2: len 7; hex d3000002a10110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
-
------------------
-
---lock strcut(1):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
TABLE LOCK table `test`.`testlll` trx id 323283 lock mode IX
-
---lock strcut(2):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 323283 lock mode S(LOCK_S) locks rec but not gap(LOCK_REC_NOT_GAP) waiting(LOCK_WAIT)
-
Record lock, heap no 27 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 8000001b; asc ;;
-
1: len 6; hex 00000004eed4; asc ;;
-
2: len 7; hex d3000002a10110; asc ;;
-
3: len 7; hex 67616f70656e67; asc gaopeng;;
-
-
-
---lock strcut(3):(Add by gaopeng) In modify Version I force check all REC_LOCK/TAB_LOCK for this Trx
-
RECORD LOCKS space id 434 page no 3 n bits 96 index PRIMARY of table `test`.`testlll` trx id 323283 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP)
-
Record lock, heap no 26 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
-
0: len 4; hex 8000001a; asc ;;
-
1: len 6; hex 00000004eed3; asc ;;
-
2: len 7; hex d2000002330110; asc 3 ;;
- 3: len 7; hex 67616f70656e67; asc gaopeng;;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2141893/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL insert on duplicate key update 死鎖MySql
- Mysql 可能鎖表的情況MySql
- 【Mysql】兩條select for update引起的死鎖MySql
- 常見的死鎖情況及解決方法
- 【Mysql】兩條insert 語句產生的死鎖MySql
- MySQL死鎖案例分析一(先delete,再insert,導致死鎖)MySqldelete
- mysql INSERT ... ON DUPLICATE KEY UPDATEMySql
- 記一次 MySQL select for update 死鎖問題MySql
- MySQL:RR模式下死鎖一列MySql模式
- 面試:什麼是死鎖,如何避免或解決死鎖;MySQL中的死鎖現象,MySQL死鎖如何解決面試MySql
- MySQL 死鎖和鎖等待MySql
- MySQL:RR模式下insert也可能導致查詢慢MySql模式
- 故障分析 | 從 Insert 併發死鎖分析 Insert 加鎖原始碼邏輯原始碼
- 死鎖: delete+insert不存在PKdelete
- mysql insert導致死鎖MySql
- 查詢鎖等待情況
- MySQL:一個死鎖分析 (未分析出來的死鎖)MySql
- 處理表鎖定的情況
- mysql update join,insert select 語法MySql
- mysql操作命令梳理(2)-alter(update、insert)MySql
- MySQL批量Insert應用ON DUPLICATE KEY UPDATEMySql
- mysql innodb新建索引堵塞update ,insert,deleteMySql索引delete
- mysql 在delete、insert、update 時,page的變化MySqldelete
- MySQL之資料的insert-delete-update操作MySqldelete
- MySQL 關於 INSERT INTO...ON DUPLICATE KEY UPDATE 的使用MySql
- MySQL死鎖系列-線上死鎖問題排查思路MySql
- mysql索引失效的情況MySql索引
- GreatSQL 中 Insert 慢是什麼情況?SQL
- MySQL 死鎖解決MySql
- MySQL死鎖問題MySql
- 【MySQL】漫談死鎖MySql
- MySQL解決死鎖MySql
- mysql同一個事務中update,insert導致死鎖問題分析解決MySql
- MySQL 共享鎖 (lock in share mode),排他鎖 (for update)MySql
- mysql for update是鎖表還是鎖行MySql
- mysql insert into ... select的鎖問題MySql
- PLinq不需要鎖的情況
- mysql行鎖和死鎖檢測MySql