您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在MySQL中找出未提交的事務信息,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
mysql> select connection_id() from dual; +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec) mysql> set session autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql> delete from kkk where id =1; Query OK, 1 row affected (0.00 sec) mysql>
在另外一個會話(連接)里面,查詢這個超過10秒未提交事務的詳細信息:
SELECT t.trx_mysql_thread_id ,t.trx_state ,t.trx_tables_in_use ,t.trx_tables_locked ,t.trx_query ,t.trx_rows_locked ,t.trx_rows_modified ,t.trx_lock_structs ,t.trx_started ,t.trx_isolation_level ,p.time ,p.user ,p.host ,p.db ,p.command FROM information_schema.innodb_trx t INNER JOIN information_schema.processlist p ON t.trx_mysql_thread_id = p.id WHERE t.trx_state = 'RUNNING' AND p.time > 10 AND p.command = 'Sleep'\G
如上截圖所示,trx_query 為NULL值。基本上無法找到未提交事務的SQL語句,MySQL內部關于事務的信息不是很細,甚至可以說有點簡潔。我甚至無法定位到在那個表上發生了鎖。只能看到trx_row_locked、trx_row_modified、trx_started等信息。使用show engine innodb status
也是如此,只能看到一些基本信息
mysql> show engine innodb status; ---TRANSACTION 1282583, ACTIVE 11937 sec 2 lock struct(s), heap size 360, 8 row lock(s), undo log entries 1 MySQL thread id 6, OS thread handle 0x7f8da2de3700, query id 190 localhost root
如果未提交的事務,阻塞了其它會話,那么有可能(僅僅是存在可能性,很多場景也不能找到位提交事務的相關SQL)找到未提交事務執行的SQL
如下測試所示,會話(連接 connection_id=11
)中執行了delete操作,但是未提交事務
mysql> set session autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 11 | +-----------------+ 1 row in set (0.01 sec) mysql> delete from kkk where id=1; Query OK, 1 row affected (0.00 sec) mysql>
另外一個會話(連接)執行了一個更新記錄的操作。此時SQL將被阻塞。
mysql> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 13 | +-----------------+ 1 row in set (0.00 sec) mysql> mysql> update kkk set id=100 where id=1;
我們在另外的會話中,執行下面SQL就可以查到未提交事務最后執行的SQL。
mysql> SELECT r.trx_id waiting_trx_id, -> r.trx_mysql_thread_id waiting_thread, -> r.trx_query waiting_query, -> b.trx_id blocking_trx_id, -> b.trx_mysql_thread_id blocking_thread, -> b.trx_query blocking_query -> FROM information_schema.innodb_lock_waits w -> INNER JOIN information_schema.innodb_trx b -> ON b.trx_id = w.blocking_trx_id -> INNER JOIN information_schema.innodb_trx r -> ON r.trx_id = w.requesting_trx_id; +----------------+----------------+----------------------------------+-----------------+-----------------+----------------+ | waiting_trx_id | waiting_thread | waiting_query | blocking_trx_id | blocking_thread | blocking_query | +----------------+----------------+----------------------------------+-----------------+-----------------+----------------+ | 2830 | 13 | update kkk set id=100 where id=1 | 2825 | 11 | NULL | +----------------+----------------+----------------------------------+-----------------+-----------------+----------------+ 1 row in set (0.00 sec) mysql> SELECT a.sql_text, -> c.id, -> d.trx_started -> FROM performance_schema.events_statements_current a -> join performance_schema.threads b -> ON a.thread_id = b.thread_id -> join information_schema.processlist c -> ON b.processlist_id = c.id -> join information_schema.innodb_trx d -> ON c.id = d.trx_mysql_thread_id -> where c.id=11 -> ORDER BY d.trx_started\G; *************************** 1. row *************************** sql_text: delete from kkk where id =1 id: 11 trx_started: 2019-06-12 23:36:13 1 row in set (0.03 sec) ERROR: No query specified mysql>
以上就是怎么在MySQL中找出未提交的事務信息,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。