1. Reasons for MySQL table locking
Table locking refers to a session locking a table, causing other sessions to be unable to access or modify the data of the table. Here are common causes of table locking in MySQL:
1. Explicit lock table
MySQL providesLOCK TABLES
Statement that allows the user to manually lock the table. For example:
LOCK TABLES table_name READ; -- Add reading lock LOCK TABLES table_name WRITE; -- Write lock
After explicitly locking the table, other sessions cannot write (read lock) or any operation (write lock) to the table until the lock is released.
2. Locks in transactions
In a transaction, MySQL locks tables or rows based on isolation level and operation type:
- Row-level lock: InnoDB engine supports row-level locks, for example:
SELECT * FROM table_name WHERE id = 1 FOR UPDATE; -- Add exclusive lock SELECT * FROM table_name WHERE id = 1 LOCK IN SHARE MODE; -- Add a shared lock
-
Table-level lock: MyISAM engine performs write operations (such as
UPDATE
、DELETE
、INSERT
) will lock the entire table.
3. Deadlock
When multiple transactions wait for each other to release the lock, it will cause a deadlock. For example:
- Transaction A locks Table 1 and tries to lock Table 2;
- Transaction B locks Table 2 and tries to lock Table 1.
At this time, neither transaction can continue to execute, resulting in table locking.
4. Long-running transactions
If a transaction is not committed or rolled back for a long time, the lock it holds will remain, blocking other operations.
5. High concurrent write operation
In high concurrency scenarios, a large number of write operations can lead to lock contention, especially in storage engines using table-level locks (such as MyISAM).
6. Indexing issues
If the query does not use the appropriate index, MySQL may perform a full table scan, which increases the probability of lock conflict.
7. Lock upgrade
In some cases, MySQL may upgrade row-level locks to table-level locks, increasing the likelihood of lock conflicts.
8. DDL operation
implementALTER TABLE
When waiting for the DDL statement, MySQL will lock the table until the operation is completed.
9. Insufficient system resources
If the system memory or CPU resources are insufficient, it may cause lock release delay, thereby extending the time for table locking.
10. Lock waiting timeout
If a transaction waits for locking for more thaninnodb_lock_wait_timeout
MySQL will throw a timeout error.
2. How to detect table locks in MySQL
When the database performance degrades or a timeout error occurs, we need to check whether any tables are locked. Here are some commonly used detection methods:
1. Use the SHOW OPEN TABLES command
SHOW OPEN TABLES
The command can display the currently open table and its status. ifIn_use
If the value of the column is greater than 0, it means that the table is locked.
SHOW OPEN TABLES WHERE In_use > 0;
2. Use information_schema.INNODB_LOCKS and INNODB_LOCK_WAITS
For the InnoDB engine, you can view lock information and lock waiting information by querying the INNODB_LOCKS and INNODB_LOCK_WAITS tables in the information_schema library.
-- View the current lock information SELECT * FROM information_schema.INNODB_LOCKS; -- View lock waiting information SELECT * FROM information_schema.INNODB_LOCK_WAITS;
3. Use information_schema.INNODB_TRX
INNODB_TRX
The table records the currently running transaction and its lock information. By querying the table, you can see which transactions hold locks.
SELECT * FROM information_schema.INNODB_TRX;
4. Use SHOW ENGINE INNODB STATUS
SHOW ENGINE INNODB STATUS
The command provides detailed InnoDB status information, including lock information.
SHOW ENGINE INNODB STATUS;
5. Use SHOW FULL PROCESSLIST
SHOW FULL PROCESSLIST
The command can view all current connections and their status. If a connectedState
The column displays "Locked", indicating that the connection is waiting for lock.
SHOW FULL PROCESSLIST;
6. Use performance_schema (MySQL 5.6 and above)
In MySQL 5.6 and above,performance_schema
The library provides more detailed lock information. Can be queriedmetadata_locks
table to view metadata locks.
SELECT * FROM performance_schema.metadata_locks;
7. Comprehensive query example
Here is a comprehensive query example to see which transactions are waiting for locks and which transactions block them:
SELECT r.trx_id AS waiting_trx_id, r.trx_mysql_thread_id AS waiting_thread, r.trx_query AS waiting_query, b.trx_id AS blocking_trx_id, b.trx_mysql_thread_id AS blocking_thread, b.trx_query AS blocking_query FROM information_schema.INNODB_LOCK_WAITS w INNER JOIN information_schema.INNODB_TRX b ON b.trx_id = w.blocking_trx_id INNER JOIN information_schema.INNODB_TRX r ON r.trx_id = w.requesting_trx_id;
3. Methods to solve MySQL table locking
1. Optimize query
- Make sure the query uses the appropriate index and avoid full table scanning.
- Reduce lock holding time and try to make transactions commit or rollback as quickly as possible.
2. Split transactions
- Split large transactions into multiple small transactions to reduce lock holding time.
3. Use row-level locks
- Try to use the InnoDB engine and use row-level locks to reduce lock conflicts.
4. Monitoring and tuning
- Use monitoring tools such as Performance Schema to check locks regularly.
- Adjust MySQL parameters (e.g.
innodb_lock_wait_timeout
) to meet business needs.
5. Avoid deadlocks
- Access tables in fixed order in code to reduce the possibility of deadlocks.
6. Upgrade the hardware
- If the system resources are insufficient, consider upgrading the hardware (such as adding memory or CPU).
4. Summary
Table locking is a common problem in MySQL, especially in high concurrency scenarios. By understanding the reasons for table locking, mastering detection methods and adopting effective solutions, the performance and stability of the database can be significantly improved. In actual work, it is recommended to monitor the locking situation of the database regularly and optimize query and transaction design according to business needs to avoid performance problems caused by table locking.
I hope this article can help you better understand and solve the table locking problem in MySQL!
This is the article about the causes, detection and solutions of MySQL table locking problems. For more information about MySQL table locking problems, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!