Home >Database >Mysql Tutorial >How to Identify Tables Locked by LOCK TABLE in MySQL?
Querying Locked Tables in MySQL
While MySQL provides various mechanisms for locking tables, detecting tables locked specifically by the LOCK TABLE command can be a crucial task for resolving performance bottlenecks. This article delves into a reliable method for identifying locked tables.
Retrieving Locked Table Information
Unlike named locks acquired with GET_LOCK, tables locked using LOCK TABLE do not have a simple mechanism for querying. However, leveraging the SHOW OPEN TABLES statement provides a comprehensive solution for extracting this information.
Using SHOW OPEN TABLES
The SHOW OPEN TABLES statement returns detailed information about all open tables in the current database connection. This includes tables actively being read from or written to, as well as tables locked by LOCK TABLE. By using the LIKE operator on the Table and Database columns, you can filter the results for a specific table and database. Additionally, the In_use column indicates the number of concurrent connections currently using the table.
Example Query
To identify locked tables in a specific database, execute the following query:
<code class="sql">SHOW OPEN TABLES WHERE `Table` LIKE '%[TABLE_NAME]%' AND `Database` LIKE '[DBNAME]' AND In_use > 0;</code>
Replace [TABLE_NAME] with the name of the table you want to check and [DBNAME] with the database in which it resides. This query will return a row for each locked table matching the criteria.
The above is the detailed content of How to Identify Tables Locked by LOCK TABLE in MySQL?. For more information, please follow other related articles on the PHP Chinese website!