当前位置:网站首页>[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
边栏推荐
猜你喜欢

Qt+vtk+occt reading iges/step model

Niuniu's team competition

"Jianzhi offer 04" two-dimensional array search

vulnhub之Ripper

Excel快速跨表复制粘贴

Why can't my MySQL container start

Modular programming of single chip microcomputer

Vulnhub pyexp
![Capturing and sorting out external Fiddler -- Conversation bar and filter [2]](/img/04/e9cc027d753e7049f273d866eefdce.png)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]

ArcGIS application (XXI) ArcMap method of deleting layer specified features
随机推荐
vulnhub之Ripper
网络通讯之Socket-Tcp(一)
vulnhub之Nagini
Systemverilog-- OOP--对象的拷贝
STL教程8-map
安裝electron失敗的解决辦法
Test classification in openstack
Extrapolated scatter data
PHP导出word方法(一phpword)
Yintai department store ignites the city's "night economy"
Dynamically monitor disk i/o with ZABBIX
Nestjs configuration service, configuring cookies and sessions
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
Differences between MySQL Union and union all
Qt OpenGL 纹理贴图
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
PHP Basics
libvirt 中体验容器
(数据库提权——Redis)Redis未授权访问漏洞总结