当前位置:网站首页>[official MySQL document] deadlock
[official MySQL document] deadlock
2022-07-03 11:59:00 【Programmer Xiao Li】
A deadlock is a situation where different transactions are unable to proceed because each holds a lock that the other needs. Because both transactions are waiting for a resource to become available, neither ever release the locks it holds.
Deadlock is generated in this way : Different transactions hold the locks needed by each other , And wait for the other party to release the lock .
One select ··· lock in share mode And delete Deadlock instance of
Create a table , And insert data :
mysql> CREATE TABLE t (i INT) ENGINE = InnoDB;
Query OK, 0 rows affected (1.07 sec)
mysql> INSERT INTO t (i) VALUES(1);
Query OK, 1 row affected (0.09 sec)
Business A | Business B | result |
select * from t where i = 1 lock in share mode; | Get S lock | |
delete from t where i = 1; | Try to get X lock , For business A Hold the read lock of the row , So enter the lock waiting queue . | |
delete from t where i = 1; | Deadlock occurs , Here is the transaction A Trying to get a write lock , But the business B In front of the waiting queue , Business A Cannot upgrade a read lock to a write lock .A wait for B Release the write lock ,B wait for A Release read lock . |
When update、select for update Different tables are locked in different order , It's prone to deadlock .
In the same table , Two transactions lock different records or gaps in different order , It's also deadlocked .
InnoDB Use automatic row level locking . Even in only insert or delete In a row of transactions , There can also be deadlocks . This is because these operations are not real “ atom ”; They will automatically insert or delete lines ( There may be more than one ) Index record setting lock .
How to avoid deadlock
1. Business is better than LOCK TABLE It will be better
2. The business is as small as possible , Avoid reading and writing large amounts of data
3. As far as possible, different transactions acquire locks in the same order (select for update)
4. stay select for update and update where Create index on
5. The possibility of deadlock has nothing to do with the isolation level of transactions , Deadlocks are related to write operations , The isolation level solves the visibility of reading
6. SHOW ENGINE INNODB STATUS You can check the cause of the deadlock .
7. When deadlocks occur frequently , Can make innodb_print_all_deadlocks
8. Use select for update , select lock in share mode, The recommended isolation level is RC
9. Operate multiple tables , Or different rows of the same table , It is recommended to use the same sequence , Deadlock can be avoided
10. Set the appropriate index , Avoid scanning a large number of data rows
11. Use as common as possible select instead of for update, Avoid locking
12. Serialization is the final solution
边栏推荐
- Redis notes 01: Introduction
- Ripper of vulnhub
- 836. 合并集合(DAY 63)并查集
- R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
- Go language to realize static server
- Solution à la défaillance de l'installation d'Electron
- Visual studio 2022 downloading and configuring opencv4.5.5
- Qt OpenGL相机的使用
- 2022年湖南工学院ACM集训第二次周测题解
- MCDF Experiment 1
猜你喜欢
随机推荐
R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
STL教程8-map
Socket TCP for network communication (I)
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
[learning notes] DP status and transfer
剑指offer专项32-96题做题笔记
Ripper of vulnhub
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
cgroup简介
shardingSphere分库分表<3>
Visual Studio 2022下载及配置OpenCV4.5.5
typeScript
Nestjs configuration service, configuring cookies and sessions
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
错排问题 (抽奖,发邮件)
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
(数据库提权——Redis)Redis未授权访问漏洞总结
Master and backup role election strategy in kept
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中