当前位置:网站首页>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).
边栏推荐
- Fragment lazy load
- The company cannot access station B
- Classified by the number of 1 in binary number
- 35. Search the insertion position
- In depth interpretation of happens before principle
- 像素和内存的关系
- QT——LCDNumber
- SCADA and three industrial control systems PLC, DCS and FCS
- MySQL transaction details
- -bash: ./build. Sh: /bin/bash^m: bad interpreter: no that file or directory
猜你喜欢
![[报错]Exception: Found duplicate column(s) in the data schema: `value`;](/img/df/ca676633ca6d5e8c0a870be0732707.png)
[报错]Exception: Found duplicate column(s) in the data schema: `value`;

The combination of pytest confitest.py and fixture

SparkSQL的UDF及分析案例,220725,

Pytest fixture decorator

C#笔记

pytest pytest. Ini configuration case grouping case skipping

Bash shell learning notes (V)

看源码之LinkedList

easyui05

Pyqt5 rapid development and practice 3.1 QT designer quick start
随机推荐
SparkSQL的UDF及分析案例,220725,
3dunity game project practice - first person shooting game
C#笔记
win10 1903 笔记本开热点出现蓝屏问题
Reproduce PHP one sentence Trojan horse
Esxi6.5 patch update
ThreadPoolExecutor是怎样执行任务的
Logging advanced use
找工作4个月,面试15家,终于拿到3个offer,定级P7+
承认吧 投新能源就是为了安全感
The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
MySQL死锁分析
Linkedblockingqueue of novice source code
Wireshark basic tutorial Ethernet frame analysis.
logging 高级使用
-bash: ./build. Sh: /bin/bash^m: bad interpreter: no that file or directory
leetcode-209. 长度最小的子数组(二分、前缀和、滑动窗口)
MySql基础知识汇总
Pre post pytest method
QT——LCDNumber