# 监控mysql事务执行时间
# 长事务可能会扣你绩效的
长事务在数据库中可能会导致你各种问题, 有可能会出现锁超时, 应用性能大幅度降低等现象.
所以在开发中, 需要时刻注意两点: 1控制事务长度 2减小锁粒度
# 你需要监控长事务的发生
长事务其实在代码的管控中是不好监控的, 如果只是在审核代码时去管控, 很难保证效果, 难免有漏网之鱼, 因此也需要能监控线上长事务的发生.
# 查询数据库中长事务
select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>20
# 在上一句的基础上增加与processlist表的关联, 获取到长事务的当前db和状态
select trx_state,trx_mysql_thread_id,b.*,TIME_TO_SEC(timediff(now(),trx_started)) from information_schema.innodb_trx a
inner join information_schema.PROCESSLIST b on b.ID = a.trx_mysql_thread_id
where TIME_TO_SEC(timediff(now(),trx_started))>5