您好,登錄后才能下訂單哦!
本篇內容主要講解“如何找到上鎖的SQL語句”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何找到上鎖的SQL語句”吧!
問題
有的時候 SQL 語句被鎖住了,可是通過 show processlist 找不到加鎖的的 SQL 語句,這個時候應該怎么排查呢
前提
performance_schema = on;
實驗
1、建一個表,插入三條數據
mysql> use test1; Database changed mysql> create table action1(id int); Query OK, 0 rows affected (0.11 sec) mysql> insert into action1 values(1),(2),(3); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from action1; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+3 rows in set (0.00 sec)
2、開啟一個事務,刪除掉一行記錄,但不提交
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> delete from action1 where id = 3; Query OK, 1 row affected (0.00 sec)
3、另開啟一個事務,更新這條語句,會被鎖住
mysql> update action1 set id = 7 where id = 3;
4、通過 show processlist 只能看到一條正在執行的 SQL 語句
mysql> show processlist; | 22188 | root | localhost | test1 | Sleep | 483 | | NULL | | 22218 | root | localhost | NULL | Query | 0 | starting | show processlist | | 22226 | root | localhost | test1 | Query | 3 | updating | update action1 set id = 7 where id = 3 | +-------+-------------+--------------------+-------+---------+------+----------+----------------------------------------+
5、接下來就是我們知道的,通過 information_schema 庫里的 INNODBTRX、INNODBLOCKS 、INNODBLOCK_WAITS 獲得的一個鎖信息
mysql> select * from INNODB_LOCK_WAITS; +-------------------+-------------------+-----------------+------------------+ | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id | +-------------------+-------------------+-----------------+------------------+ | 5978292 | 5978292:542:3:2 | 5976374 | 5976374:542:3:2 | +-------------------+-------------------+-----------------+------------------+1 row in set, 1 warning (0.00 sec) mysql> select * from INNODB_LOCKs; +-----------------+-------------+-----------+-----------+-------------------+-----------------+------------+-----------+----------+----------------+ | lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data | +-----------------+-------------+-----------+-----------+-------------------+-----------------+------------+-----------+----------+----------------+ | 5978292:542:3:2 | 5978292 | X | RECORD | `test1`.`action1` | GEN_CLUST_INDEX | 542 | 3 | 2 | 0x00000029D504 | | 5976374:542:3:2 | 5976374 | X | RECORD | `test1`.`action1` | GEN_CLUST_INDEX | 542 | 3 | 2 | 0x00000029D504 | +-----------------+-------------+-----------+-----------+-------------------+-----------------+------------+-----------+----------+----------------+2 rows in set, 1 warning (0.00 sec) mysql> select trx_id,trx_started,trx_requested_lock_id,trx_query,trx_mysql_thread_id from INNODB_TRX; +---------+---------------------+-----------------------+----------------------------------------+---------------------+ | trx_id | trx_started | trx_requested_lock_id | trx_query | trx_mysql_thread_id | +---------+---------------------+-----------------------+----------------------------------------+---------------------+ | 5978292 | 2020-07-26 22:55:33 | 5978292:542:3:2 | update action1 set id = 7 where id = 3 | 22226 | | 5976374 | 2020-07-26 22:47:33 | NULL | NULL | 22188 | +---------+---------------------+-----------------------+----------------------------------------+---------------------+
6、從上面可以看出來是 thread_id 為 22188 的執行的 SQL 語句鎖住了后面的更新操作,但是我們從上文中 show processlist 中并未看到這條事務,測試環境我們可以直接 kill 掉對應的線程號,但如果是生產環境中,我們需要找到對應的 SQL 語句,根據相應的語句再考慮接下來應該怎么處理
7、需要結合 performance_schema.threads 找到對應的事務號
mysql> select * from performance_schema.threads where processlist_ID = 22188\G *************************** 1. row *************************** THREAD_ID: 22225 //perfoamance_schema中的事務計數器 NAME: thread/sql/one_connection TYPE: FOREGROUND PROCESSLIST_ID: 22188 //從show processlist中看到的id PROCESSLIST_USER: root PROCESSLIST_HOST: localhost PROCESSLIST_DB: test1 PROCESSLIST_COMMAND: Sleep PROCESSLIST_TIME: 1527 PROCESSLIST_STATE: NULL PROCESSLIST_INFO: NULL PARENT_THREAD_ID: NULL ROLE: NULL INSTRUMENTED: YES HISTORY: YES CONNECTION_TYPE: Socket THREAD_OS_ID:8632 1 row in set (0.00 sec)
8、找到事務號,可以從 events_statements_current 找到對應的 SQL 語句:SQL_TEXT
mysql> select * from events_statements_current where THREAD_ID = 22225\G *************************** 1. row *************************** THREAD_ID: 22225 EVENT_ID: 14 END_EVENT_ID: 14 EVENT_NAME: statement/sql/delete SOURCE: TIMER_START: 546246699055725000 TIMER_END: 546246699593817000 TIMER_WAIT: 538092000 LOCK_TIME: 238000000 SQL_TEXT: delete from action1 where id = 3 //具體的sql語句 DIGEST: 8f9cdb489c76ec0e324f947cc3faaa7c DIGEST_TEXT: DELETE FROM `action1` WHERE `id` = ? CURRENT_SCHEMA: test1 OBJECT_TYPE: NULL OBJECT_SCHEMA: NULL OBJECT_NAME: NULL OBJECT_INSTANCE_BEGIN: NULL MYSQL_ERRNO: 0 RETURNED_SQLSTATE: 00000 MESSAGE_TEXT: NULL ERRORS: 0 WARNINGS: 0 ROWS_AFFECTED: 1 ROWS_SENT: 0 ROWS_EXAMINED: 3CREATED_TMP_DISK_TABLES: 0 CREATED_TMP_TABLES: 0 SELECT_FULL_JOIN: 0 SELECT_FULL_RANGE_JOIN: 0 SELECT_RANGE: 0 SELECT_RANGE_CHECK: 0 SELECT_SCAN: 0 SORT_MERGE_PASSES: 0 SORT_RANGE: 0 SORT_ROWS: 0 SORT_SCAN: 0 NO_INDEX_USED: 0 NO_GOOD_INDEX_USED: 0 NESTING_EVENT_ID: NULL NESTING_EVENT_TYPE: NULL NESTING_EVENT_LEVEL: 01 row in set (0.00 sec)
9、可以看到是一條 delete 阻塞了后續的 update,生產環境中可以拿著這條 SQL 語句詢問開發,是不是有 kill 的必要。
到此,相信大家對“如何找到上鎖的SQL語句”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。