当前位置:网站首页>Row lock analysis and deadlock

Row lock analysis and deadlock

2022-06-26 17:56:00 Time is light, you are safe

One 、 Row lock

clear through InnoDB_row_lock State variables are used to analyze the contention of row locks on the system :

show status like'innodb_row_lock%';

The description of each state quantity is as follows :
Innodb_row_lock_current_waits: The number of currently waiting locks
Innodb_row_lock_time: The total length of time from system startup to lock up
Innodb_row_lock_time_avg: The average time it takes to wait
Innodb_row_lock_time_max: The longest waiting time from system startup to now
Innodb_row_lock_waits: The total number of times the system has been waiting since it was started

For this 5 Two state variables , The more important thing is :
Innodb_row_lock_time_avg ( Average waiting time )
Innodb_row_lock_waits ( Total waiting times )
Innodb_row_lock_time( The total waiting time )

Especially when waiting times are high , And every time the waiting time is not small , We need an analysis system Why is there so much waiting in , Then according to the results of the analysis to develop optimization plan .

Two 、 Deadlock

Set transaction level to mysql Default level :

set tx_isolation='repeatable-read'; 

Session_1 perform :

select * from account where id=1 for update; 

Session_2 perform :

select * from account where id=2 for update; 

 Insert picture description here

Session_1 perform :

select * from account where id=2 for update; 

 Insert picture description here
Session_2 perform :

select * from account where id=1 for update;

 Insert picture description here
In most cases mysql Deadlock can be detected automatically and the transaction that generated the deadlock can be rolled back , But in some cases mysql Can't automatically detect deadlocks

Check the recent deadlock log information :

show engine innodb status\G; 
原网站

版权声明
本文为[Time is light, you are safe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261753537030.html