更多MySQL性能分析可参考《MySQL性能分析与优化》
PostgreSQL的连接/事务监控,可参考《PostgreSQL中查看/关闭正在执行的SQL、锁和事务》
目录
排查数据库性能,最早用的是
show processlist
,方便快捷毋庸置疑,但使用的越多,对实时链接的多维度过滤、统计需求就愈发强烈。
后来了解到了information_schema.processlist
,就再也没用过show processlist
,并激发了我对information_schema其他表的使用兴趣。
下面是这几年个人最常用的一些统计分析场景,主要涉及连接数、数据量、事务和锁的监控与分析。
连接数相关统计
统计总数
SELECT count(*) FROM information_schema.processlist;
按[用户、数据库、状态]汇总
SELECT count(*),user,db,command FROM information_schema.processlist
group by user, db,command;
按[来源]汇总
SELECT substring(host, 1, instr(host, ":")-1), count(*) from information_schema.processlist
group by substring(host, 1, instr(host, ":")-1);
已开启10秒以上的活跃连接
SELECT id,user,db,command,state,time,info FROM information_schema.processlist
where command <> 'sleep' and