innodb 獲取行鎖爭用情況
可以通過檢查InnoDB_row_lock狀態變數來分析系統上的行鎖的爭奪情況:
mysql> show status like 'innodb_row_lock%';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| InnoDB_row_lock_current_waits | 0 |
| InnoDB_row_lock_time | 0 |
| InnoDB_row_lock_time_avg | 0 |
| InnoDB_row_lock_time_max | 0 |
| InnoDB_row_lock_waits | 0 |
+-------------------------------+-------+
5 rows in set (0.01 sec)
如果發現鎖爭用比較嚴重,如InnoDB_row_lock_waits和InnoDB_row_lock_time_avg的值比較高,還可以通過設定InnoDB Monitors來進一步觀察發生鎖衝突的表、資料行等,並分析鎖爭用的原因。
具體方法如下:
mysql> CREATE TABLE innodb_monitor(a INT) ENGINE=INNODB;
Query OK, 0 rows affected (0.14 sec)
然後就可以用下面的語句來進行檢視:
mysql> Show innodb status\G;
*************************** 1. row ***************************
Type: InnoDB
Name:
Status:
…
…
------------
TRANSACTIONS
------------
Trx id counter 0 117472192
Purge done for trx's n:o < 0 117472190 undo n:o < 0 0
History list length 17
Total number of lock structs in row lock hash table 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 117472185, not started, process no 11052, OS thread id 1158191456
MySQL thread id 200610, query id 291197 localhost root
---TRANSACTION 0 117472183, not started, process no 11052, OS thread id 1158723936
MySQL thread id 199285, query id 291199 localhost root
Show innodb status
…
監視器可以通過發出下列語句來停止檢視:
mysql> DROP TABLE innodb_monitor;
Query OK, 0 rows affected (0.05 sec)
設定監視器後,在SHOW INNODB STATUS的顯示內容中,會有詳細的當前鎖等待的資訊,包括表名、鎖型別、鎖定記錄的情況等,便於進行進一步的分析和問題的確定。開啟監視器以後,預設情況下每15秒會向日志中記錄監控的內容,如果長時間開啟會導致.err檔案變得非常的巨大,所以使用者在確認問題原因之後,要記得刪除監控表以關閉監視器,或者通過使用“--console”選項來啟動伺服器以關閉寫日誌檔案。