页面刷新慢,排查步骤
1、在后台日志中找到查询慢的sql,确定是哪个库哪张表。(tail -f **.log > event.log)
2、在mysql中执行show processlist;(或者show full processlist;)查看是否有查询慢的sql,和日志里捞出来的sql一致
。
show processlist;详解
最近排查一些MySQL的问题,会经常用到 show processlist,所以在这里把这个命令总结一下,做个备忘,以备不时只需。
show processlist 是显示用户正在运行的线程,需要注意的是,除了 root 用户能看到所有正在运行的线程外,其他用户都只能看到自己正在运行的线程,看不到其它用户正在运行的线程。除非单独给这个用户赋予了PROCESS 权限。
root用户,可以看到全部线程运行情况,普通的activiti用户只能看到自己的,单独给activiti用户授PROCESS权限,(授权后需要退出重新登录)show processlist 显示的信息都是来自MySQL系统库 information_schema 中的 processlist 表。
所以使用下面的查询语句可以获得相同的结果:
select * from information_schema.processlist
了解这些基本信息后,下面我们看看查询出来的结果都是什么意思。
Id: 就是这个线程的唯一标识,当我们发现这个线程有问题的时候,可以通过 kill 命令,加上这个Id值将这个线程杀掉。前面我们说了show processlist 显示的信息时来自information_schema.processlist 表,所以这个Id就是这个表的主键。
User: 就是指启动这个线程的用户。
Host: 记录了发送请求的客户端的 IP 和 端口号。通过这些信息在排查问题的时候,我们可以定位到是哪个客户端的哪个进程发送的请求。
DB: 当前执行的命令是在哪一个数据库上。如果没有指定数据库,则该值为 NULL 。
Command: 是指此刻该线程正在执行的命令。这个很复杂,下面单独解释
下面我们单独看一下 Command 的值:
如果有歧义可以查看官方文档: 8.14.1 Thread Command Values
下面我们看看 State 的值有哪些:
… 发现有大神已经总结的超好了:MySQL SHOW PROCESSLIST协助故障诊断…
官方文档关于 State 的解释: 8.14.2 General Thread States
下面是常用到的SQL,记录下来便于使用。
按客户端 IP 分组,看哪个客户端的链接数最多
select client_ip,count(client_ip) as client_num from (select substring_index(host,':' ,1) as client_ip from processlist ) as connect_info group by client_ip order by client_num desc;
查看正在执行的线程,并按 Time 倒排序,看看有没有执行时间特别长的线程
select * from information_schema.processlist where Command != 'Sleep' order by Time desc;
找出所有执行时间超过 5 分钟的线程,拼凑出 kill 语句,方便后面查杀
select concat('kill ', id, ';') from information_schema.processlist where Command != 'Sleep' and Time > 300 order by Time desc;
3、show table status like ‘表名’;
show table status like 'customeventsum';+----------------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |+----------------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+| customeventsum | InnoDB |10 | Dynamic | 2209596 | 69 | 154140672 |0 | 473776128 | 5242880 | NULL | -01-19 16:02:43 | -09-13 10:54:43 | NULL | utf8_general_ci |NULL || |+----------------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+1 row in set (0.02 sec)
数据行数大概在:2209596
Data_length:154140672
Data_free:5242880
表空间碎片很小,不用做碎片清理.
4、主要是针对查询出来的表进行分析,是否添加索引和清理碎片。
5、添加索引语句
create index ind_cross_customeventuser_1 on cross_customeventuser(developerid,productid,eventId,starttime_day);
6、清理碎片语句
经常用到查看碎片的sql:
SELECT table_schema,TABLE_NAME , concat(data_free/1024/1024/1024,"G") as free,concat(data_length/1024/1024/1024,"G") as data FROM `information_schema`.tables WHERE data_free >8*1024*1024 AND ENGINE ='innodb' ORDER BY data_free DESC;
清理碎片的sql
optimize table 表名
如果觉得《页面响应时间长 排查步骤》对你有帮助,请点赞、收藏,并留下你的观点哦!