show processlist找出执行时间最长的进程
show [full] processlist;
展示属于当前用户的线程,如果当前用户有 process privilege,那么会展示所有线程。
如果不加full,只显示前100条;show processlist展示的线程不包含background thread,所以线程数就等于connections
除了上述方式,我们还可以通过show status 查看连接,比如使用
show status like ‘%threads_%’;threads_connected就是连接数就是当前线程数。
查看被打开的表
show open tables where in_use>0;
这里有个疑问,这个sql查出来的表,不一定是被锁住的。因为用查询,如果耗费时间很长,也会查询出来。
这里的open tables是什么意思? 每次操作(查询、更新、增加、删除)都会打开表,一个线程一个connection
为了维护不同的状态,他们分别打开表,这就是open tables,所以说open tables说明在操作表,如果表锁了,这个sql是正确的。
可以使用 show status like ‘%Opened_tables%’;查看已经打开的表
活学活用show status和show variables,一个是查看mysql当前状态,一个是查看环境变量;
KILL [CONNECTION | QUERY] processlist_id
不加modifier等同于kill connection
kill query pid,是杀死connection中的sql,不杀死query
总结
先通过show processlist查看耗时最长的进程,找到表;
然后再使用show open tables where in_use>0 验证一下;
确认后,使用kill process_id 杀死connection
参考