Home > Database > Mysql Tutorial > body text

Discuss in detail mysql query locked table

PHPz
Release: 2023-04-17 09:56:14
Original
965 people have browsed it

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.

  1. Use command line tools to query locked tables
    Use the SHOW PROCESSLIST command on the command line to query the currently running processes and threads. Because every table in MySQL has a thread ID associated with it, you can use this command to determine which table is currently locked. For example, the following command queries all running queries and finds which query is locking the specified table:
SHOW PROCESSLIST;
Copy after login

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.

  1. Query system views and determine locked tables
    MySQL also provides a large number of system views for checking currently running threads, lock information and mutexes, sessions and their properties , database status, etc. Query the following view to determine the currently locked table:
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS WHERE LOCK_TABLE LIKE '%table_name%';
Copy after login

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.

  1. Query the info schema table to determine which table is locked
    The info schema table in MySQL allows users to check the various resources being used by the database server. This includes open tables, current thread, locked rows and tables, time, and more. You can find the currently locked table by querying the following table:
SELECT * FROM information_schema.TABLES WHERE ENGINE = 'InnoDB' AND TABLE_NAME = 'table_name';
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!