MySQL通過performance_schema定位未提交事務所執行的SQL

神諭丶發表於2017-09-19
經常會遇到這樣一個場景:
業務那邊覺得資料庫“很慢”,上去通過show processlist檢視發現大量State為在等待lock,如:
Waiting for table metadata/level lock等


比如在執行一個DDL時,發現被hang住,檢視到目前程式狀態,有MDL

  1. mysql> SHOW PROCESSLIST;
  2. +-----+------+-----------+------+---------+------+---------------------------------+--------------------------------------------+
  3. | Id  | User | Host      | db   | Command | Time | State                           | Info                                       |
  4. +-----+------+-----------+------+---------+------+---------------------------------+--------------------------------------------+
  5. | 585 | root | localhost | test | Sleep   | 1658 |                                 | NULL                                       |
  6. | 586 | root | localhost | test | Query   | 1654 | Waiting for table metadata lock | alter table t change name name varchar(32) |
  7. | 590 | root | localhost | test | Query   | 0    | starting                        | show processlist                           |
  8. +-----+------+-----------+------+---------+------+---------------------------------+--------------------------------------------+
  9. 3 rows in set (0.00 sec)

在實驗環境裡,我們很快就能定位到,應該是id為585這個執行緒,但是無法知道正在執行什麼sql:
通過information_schema.innodb_trx\G,也不能查詢到具體執行了什麼sql。
通過簡單的kill的確可以解決眼前的問題,但如果繼續遇到該問題,也難以定位具體內容。


但其實,performance_schema.events_statements_current提供了相關資訊,此處復現一下:

  1. session1> BEGIN;
  2. Query OK, 0 rows affected (0.00 sec)

  3. session1> UPDATE t SET name='fasdfsad';
  4. Query OK, 3 rows affected (0.00 sec)
  5. Rows matched: 3 Changed: 3 Warnings: 0


  1. session2> ALTER TABLE t CHANGE name name varchar(32)
發現被hang住


檢視一下是否有事務未提交,可以發現的確有:
該事務內的語句執行完畢(處於Sleep),但未提交,就會看不到對應的trx_query:

  1. session3> SELECT * FROM information_schema.innodb_trx\G
  2. *************************** 1. row ***************************
  3.                     trx_id: 9614
  4.                  trx_state: RUNNING
  5.                trx_started: 2017-09-19 15:58:05
  6.      trx_requested_lock_id: NULL
  7.           trx_wait_started: NULL
  8.                 trx_weight: 2
  9.        trx_mysql_thread_id: 585
  10.                  trx_query: NULL
  11.        trx_operation_state: NULL
  12.          trx_tables_in_use: 0
  13.          trx_tables_locked: 1
  14.           trx_lock_structs: 2
  15.      trx_lock_memory_bytes: 1136
  16.            trx_rows_locked: 4
  17.          trx_rows_modified: 0
  18.    trx_concurrency_tickets: 0
  19.        trx_isolation_level: REPEATABLE READ
  20.          trx_unique_checks: 1
  21.     trx_foreign_key_checks: 1
  22. trx_last_foreign_key_error: NULL
  23.  trx_adaptive_hash_latched: 0
  24.  trx_adaptive_hash_timeout: 0
  25.           trx_is_read_only: 0
  26. trx_autocommit_non_locking: 0
  27. 1 row in set (0.00 sec)


只能根據trx_mysql_thread_id看到未提交的事務的process id,看一下processlist,INFO內也沒有具體內容:


  1. session3> SHOW PROCESSLIST;
  2. +-----+------+-----------+------+---------+------+---------------------------------+--------------------------------------------+
  3. | Id  | User | Host      | db   | Command | Time | State                           | Info                                       |
  4. +-----+------+-----------+------+---------+------+---------------------------------+--------------------------------------------+
  5. | 585 | root | localhost | test | Sleep   | 42   |                                 | NULL                                       |
  6. | 586 | root | localhost | test | Query   | 37   | Waiting for table metadata lock | ALTER TABLE t CHANGE name name varchar(32) |
  7. | 590 | root | localhost | test | Query   | 0    | starting                        | SHOW PROCESSLIST                           |
  8. +-----+------+-----------+------+---------+------+---------------------------------+--------------------------------------------+
  9. 3 rows in set (0.00 sec)


但只要開啟了P_S,就可以通過performance_schema.events_statements_current來檢視到對應的sql,包括已經執行完,但沒有提交的。

  1. session3> SELECT sql_text FROM performance_schema.events_statements_current;
  2. +-------------------------------------------------------------------+
  3. | sql_text                                                          |
  4. +-------------------------------------------------------------------+
  5. | UPDATE t SET name='fasdfsad'                                      |
  6. | ALTER TABLE t CHANGE name name varchar(32)                        |
  7. | select sql_text from performance_schema.events_statements_current |
  8. +-------------------------------------------------------------------+
  9. 3 rows in set (0.00 sec)
當然,在複雜的生產環境中,光憑上面的語句查出來的資訊,是遠遠不夠的。

通過如下語句,可以擴充套件show processlist的顯示結果,並提供對應的SQL。

  1. SELECT b.processlist_id, c.db, a.sql_text, c.command, c.time, c.state
  2. FROM performance_schema.events_statements_current a JOIN performance_schema.threads b USING(thread_id)
  3. JOIN information_schema.processlist c ON b.processlist_id = c.id
  4. WHERE a.sql_text NOT LIKE '%performance%';

結果:
  1. +----------------+------+--------------------------------------------+---------+------+---------------------------------+
  2. | processlist_id | db   | sql_text                                   | command | time | state                           |
  3. +----------------+------+--------------------------------------------+---------+------+---------------------------------+
  4. | 585            | test | UPDATE t SET name='fasdfsad'               | Sleep   | 243  |                                 |
  5. | 586            | test | ALTER TABLE t CHANGE name name varchar(32) | Query   | 238  | Waiting for table metadata lock |
  6. +----------------+------+--------------------------------------------+---------+------+---------------------------------+
  7. 2 rows in set (0.01 sec)
也可以很容易定位到執行的內容是做了一個update操作未提交。


作者微信公眾號(持續更新)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-2145185/,如需轉載,請註明出處,否則將追究法律責任。

相關文章