search
HomeDatabaseMysql TutorialHow to solve the MySQL deadlock problem (detailed examples)

This article brings you relevant knowledge about mysql, which mainly introduces the analysis and discussion of common deadlock cases, and how to avoid deadlocks as much as possible. Suggestions, hope it helps everyone.

How to solve the MySQL deadlock problem (detailed examples)

Recommended study: mysql tutorial

1. What is deadlock

Deadlock is a concurrent system Common problems will also appear in the concurrent read and write request scenario of the database MySQL. A "deadlock" occurs when two or more transactions are waiting for each other to release the locks they already hold or are waiting for lock resources in a loop due to inconsistent locking sequences. The common error message is Deadlock found when trying to get lock....

For example, transaction A holds the X1 lock and applies for the X2 lock, transaction B holds the X2 lock and applies for the X1 lock. Transactions A and B hold locks and apply for the locks held by the other party and wait in a loop, causing a deadlock.

How to solve the MySQL deadlock problem (detailed examples)

As shown in the picture above, the resource requests of the four cars on the right caused a loop phenomenon, that is, an infinite loop, resulting in a deadlock.

From the definition of deadlock, several factors for deadlock in MySQL are:

  1. Two or more transactions

  2. Each transaction already holds a lock and applies for a new lock

  3. The lock resource can only be held by the same transaction at the same time or is incompatible

  4. Transactions wait for each other in a loop due to holding locks and applying for locks

2. InnoDB lock type

In order to analyze deadlock, we have It is necessary to have an understanding of InnoDB's lock types.

How to solve the MySQL deadlock problem (detailed examples)

MySQL InnoDB engine implements standard row-level locks: shared lock (S lock) and exclusive lock (X lock)

  1. Different transactions can add S locks to the same row of records at the same time.

  2. If a transaction adds an X lock to a certain row of records, other transactions cannot add an S lock or an X lock, resulting in lock waiting.

If transaction T1 holds the S lock of row r, then when another transaction T2 requests the lock of r, the following processing will be done:

  1. T2 requested the S lock and was allowed immediately. As a result, T1 and T2 both hold the S lock of row r

  2. T2 requested the X lock and could not be allowed immediately

If T1 holds the X lock of r, then T2's request for r's X and S locks cannot be allowed immediately. T2 must wait for T1 to release the The locks are not compatible. The compatibility of shared locks and exclusive locks is as follows:

How to solve the MySQL deadlock problem (detailed examples)

2.1. Gap lock (gap lock)

Gap lock locks a gap to prevent insertion . Assume that the index column has three values ​​​​2, 4, and 8. If 4 is locked, the two gaps (2,4) and (4,8) will also be locked at the same time. Other transactions cannot insert records with index values ​​between these two gaps. However, there is an exception to the gap lock:

  1. If the index column is a unique index, then only this record will be locked (only row locks will be added), not the lock. gap.

  2. For a joint index and it is a unique index, if the where condition only includes part of the joint index, gap locks will still be added.

2.2, next-key lock

next-key lock is actually a combination of the gap lock in front of the row lock record. Assuming that there are index values ​​10, 11, 13 and 20, then the possible next-key locks include:

(negative infinity, 10], (10, 11], (11, 13], ( 13,20], (20, positive infinity)

Under the RR isolation level, InnoDB uses next-key lock mainly to prevent phantom reading problems.

2.3, Intention lock(Intention lock)

In order to support multi-granularity locking, InnoDB allows row locks and table locks to exist at the same time. In order to support locking operations at different granularities, InnoDB supports additional A locking method called Intention Lock. Intention lock divides the locked objects into multiple levels. Intention lock means that the transaction wants to lock at a finer granularity. Intention lock is divided into two types:

  1. Intention shared lock (IS): The transaction intends to add shared locks to certain rows in the table

  2. Intentional exclusive lock (IX): The transaction intends to add exclusive locks to certain rows in the table

Since the InnoDB storage engine supports row-level locks, intention locks do not actually Blocks any request except a full table scan. The compatibility of table-level intent locks and row-level locks is as follows:

How to solve the MySQL deadlock problem (detailed examples)

2.4. Insert Intention lock (Insert Intention lock)

The insertion intention lock is a gap lock set before inserting a row of records. This lock releases a signal of the insertion method, that is, multiple When transactions are inserted into the same index gap, they do not need to wait for each other unless they are inserted into the same position in the gap. Assume that a column has index values ​​2 and 6. As long as the insertion positions of the two transactions are different (for example, transaction A inserts 3 and transaction B inserts 4), then they can be inserted at the same time.

2.5. Lock mode compatibility matrix

The horizontal direction is the lock that is held, and the vertical direction is the lock being requested:

How to solve the MySQL deadlock problem (detailed examples)

3. Reading Deadlock log

Before conducting specific case analysis, let us first understand how to read the deadlock log, and use the information in the deadlock log as much as possible to help us solve the deadlock problem.

The database scenario of the following test cases is as follows:MySQL 5.7 transaction isolation level is RR

The table structure and data are as follows:

How to solve the MySQL deadlock problem (detailed examples)

The test examples are as follows:

How to solve the MySQL deadlock problem (detailed examples)

You can view the log of the latest deadlock by executing show engine innodb status.

3.1. The log analysis is as follows:

1.***** (1) TRANSACTION: TRANSACTION 2322, ACTIVE 6 sec starting index read

The transaction number is 2322, Active for 6 seconds, starting index read indicates that the transaction status is reading data according to the index. Other common statuses are:

How to solve the MySQL deadlock problem (detailed examples)

mysql tables in use 1 indicates that the current transaction uses a table.

locked 1 means there is a table lock on the table, for DML statements it is LOCK_IX

LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)

LOCK WAIT means it is waiting for the lock, 2 lock struct(s) means trx->trx_locks The length of the lock list is 2. Each linked list node represents a lock structure held by the transaction, including table locks, record locks, and auto-increment locks. In this use case, 2locks represents IX locks and lock_mode X (Next-key lock)

1 row lock(s) represents the number of row record locks/gap locks held by the current transaction.

MySQL thread id 37, OS thread handle 140445500716800, query id 1234 127.0.0.1 root updating

MySQL thread id 37 means that the thread ID that executes the transaction is 37 (that is, the ID displayed by show processlist;)

delete from student where stuno= 5 indicates the sql being executed by transaction 1. The uncomfortable thing is that show engine innodb status cannot view the complete sql. It usually displays the sql currently waiting for the lock.

 ***** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 11 page no 5 n bits 72 index idx_stuno of table cw****.****student trx id 2322 lock_mode X waiting

RECORD LOCKS represents record locks. This content indicates that transaction 1 is waiting for the X lock of idx_stuno on table student. In this case, it is actually Next-Key Lock.

The log of transaction 2 is similar to the above analysis:

2.***** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 11 page no 5 n bits 72 index idx_stuno of table cw****.****student trx id 2321 lock_mode X

Shows that the insert of transaction 2 into student(stuno,score) values(2,10) holds a=5 Lock mode X

LOCK_gap, but we cannot see the delete from student where stuno=5 executed by transaction 2 from the log;

This also makes it difficult for the DBA to analyze based on the log alone. The root cause of the locking problem.

3.***** (2) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 11 page no 5 n bits 72 index idx_stuno of table cw**** .****student trx id 2321 lock_mode

#4. Classic case analysis

4.1. Transaction concurrent insert unique key conflict

The table structure and data are as follows:

How to solve the MySQL deadlock problem (detailed examples)

The test examples are as follows:How to solve the MySQL deadlock problem (detailed examples)

The log analysis is as follows:How to solve the MySQL deadlock problem (detailed examples)

Transaction T2 insert into t7(id,a) values ​​(26,10) statement insert is successful, holding a=10
    Exclusive row lock (Xlocks rec but no gap)
  1. Transaction T1 insert into t7(id,a) values ​​(30,10), because the first insert of T2 has already inserted the record a=10, transaction T1 insert a=10 will cause a unique key conflict, and you need to apply for a unique key for the conflict Index plus S Next-key Lock (ie lock mode S waiting) This is a
  2. gap lock
  3. will apply to lock the gap area between (,10], (10,20].

  4. Transaction T2 insert into t7(id,a) values ​​(40,9) The value of a=9 inserted by this statement is between gap lock 4-10 applied for by transaction T1, so the second insert statement of transaction T2 needs to wait for the S-Next-key Lock lock of transaction T1 to be released, and the lock_mode X locks gap before rec insert intention waiting is displayed in the log.

4.2. Concurrency deadlock problem of updating first and then inserting

The table structure is as follows, no data:

How to solve the MySQL deadlock problem (detailed examples)

Test examples are as follows:

How to solve the MySQL deadlock problem (detailed examples)

Deadlock analysis:
You can see that two transaction update records that do not exist have obtained gap locks (gap locks) one after another. , gap locks are compatible so they will not block during the update process. Both hold gap locks and then compete to insert the intention lock. When there are other sessions holding gap locks, the current session cannot apply for the insertion intention lock, resulting in a deadlock.

5. How to avoid deadlocks as much as possible

  1. Design the index reasonably, put the columns with high distinction in front of the composite index, so that business SQL can pass through the index as much as possibleLocate fewer rows and reduce lock contention.

  2. Adjust the execution order of business logic SQL to avoid update/delete SQL that holds locks for a long time in front of the transaction.

  3. AvoidLarge transactions and try to split large transactions into multiple small transactions for processing. The probability of lock conflicts in small transactions is also smaller.

  4. Access tables and rows in a fixed order. For example, for two transactions that update data, transaction A updates data in the order 1, 2; transaction B updates data in the order 2, 1. This is more likely to cause deadlock.

  5. In systems with relatively high concurrency, do not explicitly lock, especially in transactions. For example, the select ... for update statement, if it is in a transaction (start transaction is run or autocommit is set to equal 0) , then the found record will be locked.

  6. Try to search for records by primary key/index. Range search increases the possibility of lock conflicts. Do not use the database to do additional quota calculations. For example, some programs will use statements such as "select ... where ... order by rand();". Since statements like this do not use indexes, the entire table's data will be locked.

  7. Optimize SQL and table design to reduce the situation of occupying too many resources at the same time. For example, reduce the number of connected tables and decompose complex SQL into multiple simple SQLs.

Recommended learning:

mysql learning tutorial

The above is the detailed content of How to solve the MySQL deadlock problem (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.