検索
ホームページデータベースmysql チュートリアルHow to find out who is locking a table in MySQL_MySQL

MySQL is adding more tools to monitor its internals with every new release, but one thing it still lacks is a way to find out who is locking what, and therefore which transactions block which other ones. This is such a vital feature that I’m considering writing my own patch to the source! Still, it is possible, to a limited extent, to find out who’s locking resources. In this article I’ll explain how you can do that.

This article is the second in a series on how to use the innotop MySQL and InnoDB monitor.

Introduction

Here’s the situation: you are trying to update a table and every time you issue the query, it hangs until it times out and tells you the lock wait timeout was exceeded. Someone has locked the table you’re trying to update, but you have no idea who. This can be incredibly frustrating, because this could go on indefinitely. I’ve sometimes had to put work off till another day, because the table is locked all day long.

I’ve found only a very limited set of circumstances in which MySQL will say what’s happening with locks. These are all printed out in the text of SHOW ENGINE INNODB STATUS.

When there was a deadlock

The first way to see locks is when there’s been a deadlock. The status text will show transaction information on the transactions that deadlocked, which locks they held, and which they were waiting for. Here is a sample. Look at the sections titled “WAITING FOR THIS LOCK TO BE GRANTED” and “HOLDS THE LOCKS.”

------------------------LATEST DETECTED DEADLOCK------------------------060731 20:19:58*** (1) TRANSACTION:TRANSACTION 0 93698, ACTIVE 2 sec, process no 12767, OS thread id 1141946720 starting index readmysql tables in use 1, locked 1LOCK WAIT 4 lock struct(s), heap size 1216MySQL thread id 3, query id 19 localhost root Updatingupdate test.innodb_deadlock_maker set a = 0 where a <> 0*** (1) WAITING FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 0 page no 131120 n bits 72 index `GEN_CLUST_INDEX` of table `test/innodb_deadlock_maker` trx id 0 93698 lock_mode X waitingRecord lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 6; hex 000000019000; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 80000000320110; asc     2  ;; 3: len 4; hex 80000000; asc     ;;*** (2) TRANSACTION:TRANSACTION 0 93699, ACTIVE 2 sec, process no 12767, OS thread id 1142212960 starting index read, thread declared inside InnoDB 500mysql tables in use 1, locked 14 lock struct(s), heap size 1216MySQL thread id 4, query id 20 localhost root Updatingupdate test.innodb_deadlock_maker set a = 1 where a <> 1*** (2) HOLDS THE LOCK(S):RECORD LOCKS space id 0 page no 131120 n bits 72 index `GEN_CLUST_INDEX` of table `test/innodb_deadlock_maker` trx id 0 93699 lock mode SRecord lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;;Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 6; hex 000000019000; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 80000000320110; asc     2  ;; 3: len 4; hex 80000000; asc     ;;Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 6; hex 000000019001; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 8000000032011f; asc     2  ;; 3: len 4; hex 80000001; asc     ;;*** (2) WAITING FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 0 page no 131120 n bits 72 index `GEN_CLUST_INDEX` of table `test/innodb_deadlock_maker` trx id 0 93699 lock_mode X waitingRecord lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 6; hex 000000019000; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 80000000320110; asc     2  ;; 3: len 4; hex 80000000; asc     ;;*** WE ROLL BACK TRANSACTION (2)

More importantly, the lines beginning “RECORD LOCKS space id 0″ show which index of which table was locked. That is the real meat of the matter — that’s what you need to know.

There’s just one problem: after there’s been a deadlock, it’s too late. You don’t want to know what held locks in the past, you want to know what holds them now. The deadlock information isn’t usually helpful in finding out what transaction is blocking something from happening.

When a transaction is waiting for locks

The next place you can sometimes see lock information is in the transaction section of the output. Here’s a sample:

---TRANSACTION 0 93789802, ACTIVE 19 sec, process no 9544, OS thread id 389120018MySQL thread id 23740, query id 194861248 worker1.office 192.168.0.12 robot---TRANSACTION 0 93789797, ACTIVE 20 sec, process no 9537, OS thread id 389005359 starting index readmysql tables in use 1, locked 1LOCK WAIT 2 lock struct(s), heap size 320MySQL thread id 23733, query id 194861215 elpaso 192.168.0.31 robot Updatingupdate test.test set col1 = 4------- TRX HAS BEEN WAITING 20 SEC FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 0 page no 299998 n bits 200 index `PRIMARY` of table `test/test` trx id 0 93789797 lock_mode X locks rec but not gap waitingRecord lock, heap no 77 PHYSICAL RECORD: n_fields 15; compact format; info bits 0  0: len 4; hex 80474fd6; asc  GO ;; 1: len 6; hex 000005970680; asc       ;; 2: len 7; hex 000017c02b176c; asc     + l;; 3: len 4; hex 80000003; asc     ;; 4: len 8; hex 800000000da0c93a; asc        :;; 5: len 8; hex 800000000eb2ea7e; asc        ~;; 6: len 4; hex c771fe44; asc  q D;; 7: len 4; hex 8000003e; asc    >;; 8: len 8; hex 8000123eb9e5dfd5; asc    >    ;; 9: len 4; hex 8000003a; asc    :;; 10: len 8; hex 8000123eb9e43603; asc    >  6 ;; 11: len 4; hex 80000035; asc    5;; 12: len 8; hex 8000123eb9d6c130; asc    >   0;; 13: len 4; hex 80000033; asc    3;; 14: len 8; hex 8000123eb9c7c853; asc    >   S;; ------------------------TRANSACTION 0 93789679, ACTIVE 31082 sec, process no 9535, OS thread id 388972583 starting index read, thread declared inside InnoDB 6mysql tables in use 4, locked 411614 lock struct(s), heap size 683328MySQL thread id 23731, query id 194861117 elpaso 192.168.0.31 robot

Notice the first transaction has been waiting 20 seconds for a lock to be granted, and it tells you which table and index as above. The other transaction I included (there were many in this section, but I omitted most) says it has 4 tables open and 4 locked. What it doesn’t say is which ones.

Again, there’s some information here, but not a lot. If you issue a query and it hangs and waits for a lock, knowing what lock it’s waiting for isn’t really helpful. And knowing some other transaction holds a lock isn’talways helpful either.

It can be useful sometimes though, and that’s better than nothing. If you only see two transactions with locks, you know the one that’s not waiting for a lock is probably the one that holds them. Notice something scary in the information above? Transaction “0 93789771″, on connection 23731, has been active for… eight and a half hours! Whoa. It’s time to find out what owns that connection and possibly kill it.

The take-away here is, if you’re getting blocked on an InnoDB table, and you’re lucky enough to see only one other transaction with locks, it’s probably the one blocking you.

What about table locks?

Ah, good question. What if the table isn’t InnoDB, or what if someone locked it with LOCK TABLES, and it doesn’t show up in the output of SHOW ENGINE INNODB STATUS? As far as I know, you’re helpless. I don’t know how to get any information on who’s locking the table then. Table lock information doesn’t seem to be exposed in any fashion — only row lock information.

In fact, if you’re in a transaction, LOCK TABLES seems to “kick you out” of the transaction. Try experimenting with START TRANSACTION and LOCK TABLES on an InnoDB table, and you’ll see what I mean. If you lock a table for writing, then try to select from it in another connection, the other connection will block. If you then issueSTART TRANSACTION on the first connection, the second connection will immediately unblock, and the first connection’s transaction will disappear from the InnoDB status text.

Who wants to read all that mess?

Who, indeed? The text I included above is a pain to read, and it’s not even representative of what you’ll really be looking at. For one thing, you might have to scan through 40 or more transactions to find the ones you care about, and then there’s all the other information in the output, some of which can be voluminous (such as deadlocks). What a hassle!

Fortunately, there’s a tool to do that for you: innotop. This tool formats the output neatly and gives you filtering options to display only transactions with locks (or just sort them to the top of the display). Here’s how you can do that:

Start innotop and use the “T” key to enter InnoDB Transaction mode, if it’s not already in that mode. You will see a list of transactions. Next, make the “Locks,” “Tbl Used,” and “Tbl Lck” columns visible. Press the “c” key to activate the “choose columns” dialog:

Press Return and you should see something that looks like this:

Now sort transactions with locks to the top by pressing the “s” key and choosing “lock_structs” as the sort column. You may need to press the “r” key afterwards to reverse the sort order if they go to the bottom instead. Alternatively, you can use the “w” key to add a filter on the “lock_structs” column, such as “[1-9]” to match only rows where the column isn’t zero (this is a handy filter to add in general, just so you can see how many transactions have locks).

Here’s a screenshot of me changing the sort column, and adding a filter:

And here’s a screenshot of the result:

In this example you could see the locks without hiding the other rows, but when you have a very busy server it can really help to hide all the transactions without locks.

Isn’t that easier than digging through the output of SHOW ENGINE INNODB STATUS? I think so.

Is there more?

Though I’ve searched the Internet, searched the source code and the MySQL manual, I haven’t been able to find any other ways to get information on current locks in MySQL. But I’d be delighted if you prove me wrong! If you have anything to add, please comment.


原文地址:http://www.xaprb.com/blog/2006/07/31/how-to-analyze-innodb-mysql-locks/



声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQLはデータレプリケーションをどのように処理しますか?MySQLはデータレプリケーションをどのように処理しますか?Apr 28, 2025 am 12:25 AM

MySQLは、非同期、半同期、およびグループ複製の3つのモードを介してデータの複製を処理します。 1)非同期の複製パフォーマンスは高くなりますが、データが失われる可能性があります。 2)半同期複製により、データセキュリティが向上しますが、遅延が増加します。 3)グループレプリケーションは、高可用性要件に適したマルチマスターレプリケーションとフェールオーバーをサポートします。

説明ステートメントを使用してクエリパフォーマンスを分析するにはどうすればよいですか?説明ステートメントを使用してクエリパフォーマンスを分析するにはどうすればよいですか?Apr 28, 2025 am 12:24 AM

説明ステートメントは、SQLクエリのパフォーマンスを分析および改善するために使用できます。 1.説明ステートメントを実行して、クエリプランを表示します。 2。出力結果を分析し、アクセスの種類、インデックスの使用量に注意し、順序を結合します。 3.分析結果に基づいてインデックスを作成または調整し、結合操作を最適化し、フルテーブルスキャンを回避してクエリ効率を向上させます。

MySQLデータベースをバックアップして復元するにはどうすればよいですか?MySQLデータベースをバックアップして復元するにはどうすればよいですか?Apr 28, 2025 am 12:23 AM

論理バックアップにMySQLDUMPとホットバックアップにMySQLenterPriseBackupを使用することは、MySQLデータベースをバックアップする効果的な方法です。 1. mysqldumpを使用してデータベースをバックアップします:mysqldump-uroot-pmydatabase> mydatabase_backup.sql。 2。ホットバックアップにmysqlenterprisebackupを使用:mysqlbackup - user = root-password = password - backup-dir =/path/to/backupbackup。回復するときは、対応する寿命を使用します

MySQLのスロークエリの一般的な原因は何ですか?MySQLのスロークエリの一般的な原因は何ですか?Apr 28, 2025 am 12:18 AM

MySQLのクエリが遅い主な理由には、インデックスの欠落または不適切な使用、クエリの複雑さ、過剰なデータボリューム、および不十分なハードウェアリソースが含まれます。最適化の提案には以下が含まれます。1。適切なインデックスを作成します。 2。クエリステートメントを最適化します。 3.テーブルパーティションテクノロジーを使用します。 4.適切にハードウェアをアップグレードします。

mysqlのビューは何ですか?mysqlのビューは何ですか?Apr 28, 2025 am 12:04 AM

MySQLビューは、SQLクエリの結果に基づいた仮想テーブルであり、データを保存しません。 1)ビューは複雑なクエリを簡素化し、2)データセキュリティを強化し、3)データの一貫性を維持します。ビューは、テーブルのように使用できるデータベースにクエリを保存しますが、データは動的に生成されます。

MySQLと他のSQL方言の構文の違いは何ですか?MySQLと他のSQL方言の構文の違いは何ですか?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

MySQLパーティションは何ですか?MySQLパーティションは何ですか?Apr 27, 2025 am 12:23 AM

MySQLパーティション化により、パフォーマンスが向上し、メンテナンスが簡素化されます。 1)大きなテーブルを特定の基準(日付範囲など)、2)物理的に独立したファイルに物理的に分割する、3)MySQLはクエリするときに関連するパーティションに焦点を合わせることができます。

MySQLで特権をどのように許可して取り消しますか?MySQLで特権をどのように許可して取り消しますか?Apr 27, 2025 am 12:21 AM

mysqlで許可を許可および取り消す方法は? 1。grantallprivilegesondatabase_name.to'username'@'host 'などの許可を付与するために付与ステートメントを使用してください。 2。Revokeallprivilegesondatabase_name.from'username'@'host 'など、Revoke Statementを使用して、許可のタイムリーな通信を確保します。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター