mysqldumpslow-慢查询日志分析工具

mysqldumpslow-慢查询日志分析工具

mysqldumpslow 是一个 MySQL 慢查询日志的分析工具,用于解析慢查询日志文件并总结其内容。

通常,mysqldumpslow 输出时,将类似于数字和字符串的数据值分组,并抽象为 N 和 ‘S’。 要修改值的抽象行为,请使用 -a 和 -n 选项。

调用语法

mysqldumpslow [options] [log_file ...]

直接查看慢查询日志

[root@yingzai_23 mysql]$ more slow.log 
/opt/mysql/bin/mysqld, Version: 8.0.23 (MySQL Community Server - GPL). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument
# Time: 2021-03-17T08:54:22.271358+08:00
# User@Host: root[root] @ localhost []  Id:     8
# Query_time: 0.010096  Lock_time: 0.000188 Rows_sent: 1  Rows_examined: 1
use test;
SET timestamp=1615942462;
select * from t2;

mysqldumpslow 不指定选项

[root@yingzai_23 mysql]$ mysqldumpslow slow.log 

Reading mysql slow query log from slow.log
Count: 2  Time=6.74s (13s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  start group_replication

Count: 14  Time=0.03s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  insert into t2 select id from t2

Count: 1  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@localhost
  select * from t2

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=10.0 (10), root[root]@localhost
  select * from t2 limit N

指定抽象值选项

[root@yingzai_23 mysql]$ mysqldumpslow -a -n 20 slow.log 

Reading mysql slow query log from slow.log
Count: 2  Time=6.74s (13s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  start group_replication

Count: 14  Time=0.03s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  insert into t2 select id from t2

Count: 1  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@localhost
  select * from t2

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=10.0 (10), root[root]@localhost
  select * from t2 limit 10

反向排序

[root@yingzai_23 mysql]$ mysqldumpslow -a -n 20 -r slow.log 

Reading mysql slow query log from slow.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=10.0 (10), root[root]@localhost
  select * from t2 limit 10

Count: 1  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@localhost
  select * from t2

Count: 14  Time=0.03s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  insert into t2 select id from t2

Count: 2  Time=6.74s (13s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  start group_replication

显示前3个查询

[root@yingzai_23 mysql]$ mysqldumpslow -a -n 20 -s c -t 3 slow.log 

Reading mysql slow query log from slow.log
Count: 14  Time=0.03s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  insert into t2 select id from t2

Count: 2  Time=6.74s (13s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  start group_replication

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=10.0 (10), root[root]@localhost
  select * from t2 limit 10

选项

帮助选项

  • –help
    • 输出帮助信息

抽象值选项

  • -a
    • 不要将所有数字抽象为 N,将字符串抽象为 ‘S’
  • -n N
    • 名称中的抽象数字至少包含 N 位

调试选项

  • –debug, -d

    Oracle 提供的 MySQL 版本二进制文件不是使用此选项构建的。

    • 调试模式运行
    • 仅当 MySQL 是使用 WITH_DEBUG 构建的时,此选项才可用
  • –verbose, -v
    • 详细模式

连接选项

  • -h host_name
    • MySQL 服务器的主机名,用于 *-slow.log 文件名
    • 该值可以包含通配符。默认值为 * (全部匹配)
  • -i name
    • 服务器实例的名称 (如果使用 mysql.server 启动脚本)

格式选项

  • -l
    • 不要从总时间中减去锁定时间
  • -r
    • 颠倒排序顺序
  • -s sort_type
    • 如何对输出进行排序
    • 选项值
      • t,at
        • 按查询时间或平均查询时间排序
        • 默认 -s at,按平均查询时间排序
      • l,al
        • 按锁定时间或平均锁定时间排序
      • r,ar
        • 按已发送的行或已发送的平均行进行排序
      • c
        • 按计数排序
  • -t N
    • 显示输出中的前 N 个查询

筛选选项

  • -g pattern
    • 仅考虑与 (grep 样式) 模式匹配的查询
英仔
版权声明:本站原创文章,由 英仔 2021-04-21发表,共计2672字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)