当前位置:网站首页>MySQL deadlock analysis
MySQL deadlock analysis
2022-07-26 11:16:00 【qq_ five hundred and forty-seven million twenty-six thousand on】
1. What is a deadlock
Deadlock refers to that multiple transactions hold locks required by each other , And they are waiting for each other to release the lock they need , Thus, multiple transactions have been waiting .
2. Why is a deadlock formed
- Row locks of different table records conflict :
Multiple in a transaction sql When operating the records in different tables separately, it is the same as the records in these tables of other transaction operations , Circular waiting lock condition occurs . for example : Business A Delete first table_1 In the table id=1 The record of , Business B Update again table_2 In the table id=2 The record of , Business A Update again table_2 In the table id=2 The record of , Business B And then delete table_1 In the table id=1 The record of .
- Row lock conflict of the same table record :
The same records in a table are processed simultaneously in different transactions . for example : Business A Handle id by (1,2,3,4) The record of , Business B Handle id by (2,1,5,6) The record of .
- Different index lock conflicts :
Different index records are processed simultaneously in different transactions . for example : Business A When performing an ordinary index operation , The locking order of this general index corresponding to multiple clustered indexes is (1,2,3), And the business B When performing cluster index operation , The corresponding locking sequence is (2,1,4), This creates the possibility of deadlock .
- gap Lock conflict :
innodb stay RR Below grade , If different transactions contain the same interval , Deadlock will be caused by the overlapping range of the generated gap lock . for example : Business A The interval of cluster index execution is :(1,5), Business B The interval of cluster index execution is :(3,8). When a transaction A Execute to 4 Time belongs to transaction B Clearance lock range , Need to wait for a transaction B Release the clearance lock , When a transaction B Execute to 4 when , It also belongs to affairs A Clearance lock range , You also need to wait for transactions A Release the clearance lock , Then it causes these two transactions to wait for each other to release the lock , So it's a deadlock .
3. How to deal with deadlock
- Set the wait time (innodb_lock_wait_timeout=50s): When a waiting time exceeds a set threshold , Roll back one of the transactions , Another transaction can continue .( shortcoming : If the rolled back transaction updates many rows , It takes up a lot of undo log, Then it may take more time to rollback than another normally executed transaction , It's not suitable )
- Initiate deadlock detection :innodb It also provides wait-for graph Algorithm to actively detect deadlock , The database is required to store lock information linked list and transaction waiting linked list , Through these two parts of information, a graph is constructed , When each transaction requests a lock and a wait occurs, it determines whether there is a loop , If a circuit is detected in the diagram , It indicates that there is a deadlock , Now InnoDB The storage engine will choose to rollback undo The smallest amount of transactions .
4. How to avoid deadlock
- Access tables and rows in a fixed order , Avoid cross execution of the same records by different transactions .
- Big business, small business . Big business tends to deadlock , If business permits , Break up big business into small ones .
- In the same transaction , Try to lock all the resources you need at once , Reduce deadlock probability .
- Reduce isolation level . If business permits , Lowering the isolation level is also a good choice , Let's take the isolation level from RR Adjusted for RC, A lot can be avoided because gap A deadlock caused by a lock .
- Add a reasonable index to the table . You can see that if you do not index, you will add locks for each row of the table , The probability of deadlock is greatly increased .
- Commit the transaction immediately after the operation , Especially on the interactive command line .
- With a transaction , Don't use lock tables .
- It's better not to use it (SELECT … FOR UPDATE or SELECT … LOCK IN SHARE MODE).
边栏推荐
- pytest pytest. Ini configuration case grouping case skipping
- [reprint] the multivariate normal distribution
- Classic Bluetooth connection process
- 由浅入深搭建神经网络
- Drbl diskless startup + Clonezilla network backup and restore system
- 349. Intersection of two arrays
- Introduction to authoringrealm
- Bash shell learning notes (6)
- Bash shell learning notes (VII)
- List and dictionary instance applications (※)
猜你喜欢
随机推荐
Bash shell learning notes (VII)
UDF and analysis case of sparksql, 220725,
Shell script fails to execute repeatedly automatically
菜鸟看源码之HashTable
Linkedblockingqueue of novice source code
Wireshark basic tutorial Ethernet frame analysis.
Multipartfil to file
BLE之ATT请求
Newbie sees the source code arraydeque
Smart contract DAPP system development process technology
Scrapy shell出现的一个错误
静态路由和动态路由
Pyqt5 rapid development and practice Chapter 1 understanding pyqt5
[reprint] the multivariate normal distribution
菜鸟看源码之ArrayList
List and dictionary instance applications (※)
LE Audio规范概述
[开发工具] IEDA报红
The company cannot access station B
Pytest execution rules_ Basic usage_ Common plug-ins_ Common assertions_ Common parameters








