首页 公告 项目 RSS

使用mysqldumpslow分析mysql慢日志

April 25, 2022 本文有 495 个字 需要花费 1 分钟阅读

简介

mysqldumpslow 是mysql自带的分析满查询日志的工具

操作

首先开启mysql的满查询日志,mysql的满查询日志可以放在mysql的表中也可以放在文件中,下面是具体的参数

# 开启慢查询
slow_query_log = on 
# 慢查询输出到表和文件中
log_output = TABLE,FILE
# 如果查询时间超过2s就定义为是慢查询
long_query_time = 2
# 慢查询文件的路径,如果不是指定绝对路径,比如就和我下面一样写,默认就在mysql的data文件夹中比如/var/lib/mysql
slow_query_log_file = slow.log

之后重启mysql

等一段时间有慢日志之后就可以使用mysqldumpslow分析了

其实mysqldumpslow的参数也很简单

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 (aa, ae, al, ar, at, a, c, e, l, r, t), 'at' is default
                aa: average rows affected
                ae: aggregated rows examined
                al: average lock time
                ar: average rows sent
                at: average query time
                 a: rows affected
                 c: count
                 e: rows examined 
                 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

介绍两个常见用法

查询最慢的前3条

mysqldumpslow -s at -t 3 ./slow.log

查询次数最多的前3条

mysqldumpslow -s ac -t 3 slow.log

欢迎关注我的博客www.bboy.app

Have Fun