MySQL is an open source software based on a relational database management system. It is widely used in applications and provides developers with a reliable way to store data. However, because MySQL is prone to problems maintaining locked tables when processing large amounts of data, developers need to know how to query locked tables.
When developing using MySQL, you often encounter the need to query locked tables. MySQL provides a variety of ways to query the currently locked table, including using command line tools, querying system views, and querying information schema tables. Each query method is discussed in detail below and some examples are given.
SHOW PROCESSLIST;
This will display a list of all MySQL threads currently running. In the list, you can view useful information such as ID, user, host, database, command, time and status. For example, if there is a thread locking the table named "table_name", you can find the "LOCK" field in the "Command" column. In addition, in the "Info" column, you can view the SQL statement being executed by the query.
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS WHERE LOCK_TABLE LIKE '%table_name%';
In this query statement, you can replace "table_name" with the name of the table you want to query. This statement returns all rows and tables currently locked by the InnoDB engine. Additionally, you can query the INFORMATION_SCHEMA.INNODB_LOCK_WAITS view to find threads waiting for lock resources.
SELECT * FROM information_schema.TABLES WHERE ENGINE = 'InnoDB' AND TABLE_NAME = 'table_name';
In the above query, you can replace "table_name" with the name of the table you want to query. This query will return details of the specified table server and storage engine, and if the table is being locked, the lock type can be viewed in the LOCK_TYPE field.
Summary:
MySQL can query locked tables in different ways. Use the command line tool to query running threads and find threads that lock a specific table; use system views to determine ongoing operations, waiting operations, and operations to acquire locks; query the information_schema.TABLES table to find the "QUERY" column to Identifies whether the query being executed is about a lock or some other query. But it should be noted that before making changes, please read the official MySQL documentation carefully to understand all necessary knowledge and recommendations to avoid unnecessary losses.
The above is the detailed content of Discuss in detail mysql query locked table. For more information, please follow other related articles on the PHP Chinese website!