MySql查询正在进行中的事务
用法
SELECT * FROM information_schema.INNODB_TRX
这个只能查询此刻正在进行中的事务,已经完成的是查不到的
针对mysql 5.6,查看更具体的信息:
SELECT a.trx_id, a.trx_state, a.trx_started, a.trx_query, b.ID, b.USER, b.DB, b.COMMAND, b.TIME, b.STATE, b.INFO, c.PROCESSLIST_USER, c.PROCESSLIST_HOST, c.PROCESSLIST_DB, d.SQL_TEXT FROM information_schema.INNODB_TRX a LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id AND b.COMMAND = 'Sleep' LEFT JOIN PERFORMANCE_SCHEMA.threads c ON b.id = c.PROCESSLIST_ID LEFT JOIN PERFORMANCE_SCHEMA.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;
针对mysql 5.5,查看更具体的信息:
SELECT a.trx_id, a.trx_state, a.trx_started, a.trx_query, b.ID, b. USER, b. HOST, b.DB, b.COMMAND, b.TIME, b.STATE, b.INFO FROM information_schema.INNODB_TRX a LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id WHERE b.COMMAND = 'Sleep';
查看事务等待情况:
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;
表字段定义
The INFORMATION_SCHEMA INNODB_TRX Table
(https://dev.mysql.com/doc/refman/5.7/en/innodb-trx-table.html)
用法展示
1.会话1进行事务修改,修改耗时较长,在此过程中我们在另外会话来查询
BEGIN;
UPDATE salaries SET salary = 61118 WHERE emp_no % 7 = 0;
(耗时较长,迅速进行步骤2)
2.会话2查询
3.会话1的事务结束后,(未提交)再到会话2查询
(会话未结束,但已经没有正在执行的sql语句)
4.提交会话1的事务
commit;
切换到会话2来查询:
查看连接及对应的事务
SELECT * FROM information_schema.processlist p LEFT JOIN information_schema.INNODB_TRX t ON p.id = t.trx_mysql_thread_id