INNODB_TRX 表提供了信息关于在InnoDB中执行的当前的每个事务,包含是否事务是等待一个锁,当事务开始后事务正在执行的SQL语句。
mysql> desc innodb_trx;
+----------------------------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+-----------------+------+-----+---------+-------+
| trx_id | bigint unsigned | NO | | | |
| trx_started | datetime | NO | | | |
| trx_tables_in_use | bigint unsigned | NO | | | |
| trx_tables_locked | bigint unsigned | NO | | | |
| trx_rows_locked | bigint unsigned | NO | | | |
| trx_rows_modified | bigint unsigned | NO | | | |
+----------------------------+-----------------+------+-----+---------+-------+
25 rows in set (0.01 sec)
#trx_started:事务开始时间,可以判断该事务是否是长事务。
#trx_rows_modified:事务更改的行数,可以判断该事务是否是大事务。
如何判断长事务?
#查找执行时间超过30秒的事务。
select trx_id,trx_state,trx_started,trx_mysql_thread_id,
trx_query,trx_rows_modified from information_schema.innodb_trx
where TIME_TO_SEC(timediff(now(),trx_started)) >30;
如何判断大事务?
#查找修改行数数量前十的事务。
select trx_id,trx_state,trx_started,trx_mysql_thread_id,
trx_query,trx_rows_modified from information_schema.innodb_trx
order by trx_rows_modified desc limit 10;