MySQL如何找出未提交事務信息

前陣子,我寫了一篇博客ORACLE中可否找到未提交事務的SQL語句, 那麼在MySQL數據庫中,咱們可否找出未提交事務執行的SQL語句或未提交事務的相關信息呢?html

 

實驗驗證了一下,若是一個會話(鏈接)裏面有一個未提交事務,而後不作任何操做,那麼這個線程處於Sleep狀態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秒未提交事務的詳細信息:sql

 

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 

 

 

clip_image001

 

 

如上截圖所示,trx_query 爲NULL值。基本上沒法找到未提交事務的SQL語句,MySQL內部關於事務的信息不是很細,甚至能夠說有點簡潔。我甚至沒法定位到在那個表上發生了鎖。只能看到trx_row_locked、trx_row_modified、trx_started等信息。使用show engine innodb status也是如此,只能看到一些基本信息數據庫

 

mysql> show engine innodb status;session

 

 

---TRANSACTION 1282583, ACTIVE 11937 secapp

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 rootspa

 

若是未提交的事務,阻塞了其它會話,那麼有可能(僅僅是存在可能性,不少場景也不能找到位提交事務的相關SQL)找到未提交事務執行的SQL線程

 

以下測試所示,會話(鏈接 connection_id=11)中執行了delete操做,可是未提交事務code

 

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> 

 

 

clip_image002

 

 

總結:基本上MySQL只能找到未提交事務的基本信息,例如trx_mysql_thread_id等。某些場景下,咱們幾乎沒有方法找出未提交事務執行的SQL等詳細信息。搞不清未提交事務作了什麼操做!

相關文章
相關標籤/搜索