Deadlock handling method in MySQL performance testing and tuning

Table of contents

         Foreword:

  1. Deadlock detection

  2. Deadlock avoidance

  3. Deadlock solution


         Foreword:

       MySQL deadlock refers to a resource contention phenomenon that occurs when multiple sessions request the same resource at the same time, resulting in the inability of the session to continue executing. The occurrence of deadlock will cause the transaction to fail to commit or roll back, affecting the normal operation of the application. Therefore, in MySQL performance testing and tuning, deadlocks need to be dealt with.

  1. Deadlock detection

  Query related information about lock tables through SQL statements:

  (1) Query table opening status

  SHOW OPEN TABLES WHERE IN_USE> 0

  (2) Query the lock status list

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS

  (3) Query lock waiting information, where blocking_lock_id is the transaction that the current transaction is waiting for

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS

  (4) Query deadlock logs

SHOW ENGINE INNODB STATUS

  This statement can only display the latest deadlock, and cannot fully capture all deadlock information that occurs in the system. If you want to record all deadlock logs, you need to open the innodb_print_all_deadlocks parameter to record all deadlock logs to the errorlog.

  (5) Query lock waiting time

SHOW STATUS LIKE '%lock%'

  2. Deadlock avoidance

  When a deadlock occurs, it is usually due to lengthy transactions in the program of the project, or due to inappropriate isolation level settings. We need to pay attention to the following points in transaction use:

  (1) Try to keep the transaction short and concise, and submit the transaction immediately after making a series of associated update operations to reduce the possibility of deadlock. In particular, do not let associated MySQL sessions hang uncommitted transactions for a long time.

  (2) It is recommended to use a lower isolation level, such as READ COMMITTED.

  (3) When modifying multiple tables in the same transaction, or different rows in a table, perform operations in the same order each time. In order to allow transactions to form a clear lock operation queue to avoid deadlocks.

  3. Deadlock solution

  The MySQL database resolves deadlocks through two parameters: deadlock detection (innodb_deadlock_detect) and deadlock timeout (innodb_lock_wait_timeout).

  Deadlock detection (innodb_deadlock_detect): In MySQL 8.0, a new dynamic variable innodb_deadlock_detect has been added to control whether InnoDB performs deadlock detection. The default value of this parameter is ON, that is, deadlock detection is turned on. After it is enabled, InnoDB will detect whether the lock will cause a deadlock when it is locked. If it will be locked, it will roll back the transaction with the least cost.

  Deadlock timeout (innodb_lock_wait_timeout): This parameter can be used to deal with deadlocks that cannot be detected, or to avoid waiting for a long transaction for a long time.

  For high-concurrency systems, when a large number of threads wait for the same lock, deadlock detection may cause performance degradation. At this point, it may be more efficient to disable deadlock detection and instead rely on the parameter innodb_lock_wait_timeout to release transactions that occupy lock resources for a long time. That is to say, when it is confirmed that the deadlock detection function affects the performance of the system and disabling the deadlock detection will not have a negative impact, you can try to turn off the innodb_deadlock_detect option. In addition, if InnoDB deadlock detection is disabled, the value of the parameter innodb_lock_wait_timeout needs to be adjusted in time to meet actual needs.

 As someone who has been here, I also hope that you will avoid some detours. Here I will share with you some necessities on the way forward for automated testing, hoping to help you. (WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.), I believe it can make you better progress!

Just leave [Automated Test] [Automated Test Communication]: 574737577 (remark ccc) icon-default.png?t=N5K3http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=6gweEAHLIo-VjldEz9_yMjFfeBIGgEu2&authKey=MC4ik2bydrYxO5if1oNFzT6c93XUrxOxYtv7IRY OVRDNh47xpEllgkFVDlFD%2Bf6M&noverify=0&group_code= 574737577

 

 

Guess you like

Origin blog.csdn.net/Free355/article/details/131397271