mysql慢日志使用mysqldumpslow进行分析

环境:centos7、mysql5.7、慢日志

1、mysqldumpslow参数解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
mysql> show variables like '%slow_query%';  #mysql日志路径
+---------------------+--------------------------------------+
| Variable_name       | Value                                |
+---------------------+--------------------------------------+
| slow_query_log      | ON                                   |
| slow_query_log_file | /var/lib/mysql/1d0a52e2683d-slow.log |
+---------------------+--------------------------------------+
2 rows in set (0.01 sec)
[root@minion243 tmp]# mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
 
Parse and summarize the MySQL slow query log. Options are
 
  --verbose    verbose
  --debug      debug
  --help       write this text to standard output
 
  -v           verbose
  -d           debug
  -s ORDER     what to sort by (al, at, ar, c, l, r, t), 'at' is default
                al: average lock time
                ar: average rows sent
                at: average query time
                 c: count
                 l: lock time
                 r: rows sent
                 t: query time 
  -r           reverse the sort order (largest last instead of first)
  -t NUM       just show the top n queries
  -a           don't abstract all numbers to N and strings to 'S'
  -n NUM       abstract numbers with at least n digits within names
  -g PATTERN   grep: only consider stmts that include this string
  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
  -i NAME      name of server instance (if using mysql.server startup script)
  -l           don't subtract lock time from total time

  

2、访问次数最多的20个sql语句

1
2
3
4
5
6
7
8
[root@minion243 tmp]# mysqldumpslow -s c -t 20  1d0a52e2683d-slow.log
 
Reading mysql slow query log from 1d0a52e2683d-slow.log
Count: 5021  Time=13.90s (69785s)  Lock=0.00s (1s)  Rows=0.9 (4378), rkwtest[rkwtest]@3hosts
  SELECT  authentication_id,token_id,user_name,client_id,refresh_token,token,authentication  FROM oauth_access_token  WHERE (  token  like 'S' )
 
Count: 1487  Time=15.85s (23563s)  Lock=0.00s (0s)  Rows=0.9 (1379), 2users@4hosts
  select client_id, client_secret, resource_ids, scope, authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove from oauth_client_details where client_id = 'S'

3、返回记录集最多的20个sql

1
2
3
4
5
6
7
8
[root@minion243 tmp]# mysqldumpslow -s r -t 20  1d0a52e2683d-slow.log
 
Reading mysql slow query log from 1d0a52e2683d-slow.log
Count: 1  Time=10.61s (10s)  Lock=0.00s (0s)  Rows=21180.0 (21180), rkwtest[rkwtest]@[100.98.100.246]
  SELECT u_id,alias,identity_card,phone,xh,mz,xx,csrq,sg,whcd,hyzk,university_id,university_name,nj_id,nj_name,xy_id,xy_name,zy_id,zy_name,bj_id,bj_name,create_date,create_user_id,update_date,update_user_id,university_dsdm,reserve1,reserve2,reserve3,reserve4,reserve5,profiles,status  FROM biz_student_university  WHERE  university_id = 'S' AND status = 'S'
 
Count: 976  Time=15.24s (14876s)  Lock=0.00s (0s)  Rows=12.0 (11730), 2users@2hosts
  SELECT a.u_id AS uId, b.guide_id AS guideId, a.dept_id AS deptId, b.city_code AS cityCode, a.dept_level AS deptLevel

4、查看sql语句的执行计划

1
2
3
4
5
6
7
mysql> EXPLAIN SELECT  *  FROM oauth_access_token  WHERE token  like '%282f39a1-b727-4c8a-be08-3ea924139d78%';
+----+-------------+--------------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table              | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+--------------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | oauth_access_token | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 169312 |    11.11 | Using where |
+----+-------------+--------------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.01 sec)

5、查看表的index

1
2
3
4
5
6
7
8
mysql> show index from oauth_access_token;
+--------------------+------------+--------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table              | Non_unique | Key_name     | Seq_in_index | Column_name       | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+--------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| oauth_access_token |          0 | PRIMARY      |            1 | authentication_id | A         |      169208 |     NULL | NULL   |      | BTREE      |         |               |
| oauth_access_token |          1 | idx_token_id |            1 | token_id          | A         |      169319 |     NULL | NULL   | YES  | BTREE      |         |               |
+--------------------+------------+--------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

  

posted @   苍茫宇宙  阅读(1042)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示