MySQL 在mysql查询中获取锁定的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3230693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:32:02  来源:igfitidea点击:

Get locked tables in mysql query

mysql

提问by Sharpeye500

Is there any way via a MySQL query to get the locked tables? I have a C# threading application running and there are bunch of tables getting locked in the app.

有没有办法通过 MySQL 查询来获取锁定的表?我有一个 C# 线程应用程序正在运行,并且有一堆表被锁定在应用程序中。

I need to see the locked tables and analyze the code that could be locking it.

我需要查看锁定的表并分析可能锁定它的代码。

回答by Cristian

Use:

用:

SHOW OPEN TABLES

An check whether the column In_useis greater than 0. In that case, the table is locked.

检查列In_use是否大于 0。在这种情况下,表被锁定。

Examples

例子

  • List of locked tables:

    show open tables WHERE In_use > 0

  • Check whether the table tb_employeesis locked or not:

    show open tables WHERE Table LIKE 'tb_employees' AND In_use > 0

  • 锁定表列表:

    show open tables WHERE In_use > 0

  • 检查表是否tb_employees被锁定:

    show open tables WHERE Table LIKE 'tb_employees' AND In_use > 0

From the official documentation:

官方文档

In_use

The number of table locks or lock requests there are for the table. For example, if one client acquires a lock for a table using LOCK TABLE t1 WRITE, In_use will be 1. If another client issues LOCK TABLE t1 WRITE while the table remains locked, the client will block waiting for the lock, but the lock request causes In_use to be 2. If the count is zero, the table is open but not currently being used. In_use is also increased by the HANDLER ... OPEN statement and decreased by HANDLER ... CLOSE.

In_use

表的表锁或锁请求的数量。例如,如果一个客户端使用 LOCK TABLE t1 WRITE 获取表的锁,则 In_use 将为 1。如果另一个客户端在表保持锁定时发出 LOCK TABLE t1 WRITE,客户端将阻塞等待锁,但锁请求导致 In_use 为 2。如果计数为零,则表已打开但当前未被使用。In_use 也由 HANDLER ... OPEN 语句增加,由 HANDLER ... CLOSE 减少。