MySQLのチューニングとして、スロークエリを確認しインデックスを張ったりクエリ最適化を行うというアプローチを取ることは少なくないかと思います。
その際、スロークエリログを眺めるのですが、数が多ければ何がなんだかわからないよ。ということにもなりがちかと思います。
そんな時に便利なのが「mysqldumpslow」コマンドです。
ということで簡単に使い方を調べてみました。
# 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
よく使いそうなオプション
- -s al → 平均ロックタイムの長い順
- -s ar → 平均行数の多い順
- -s at → 平均実行時間の長い順
- -s c → 総クエリ数の多い順
- -s l → 総ロックタイムの長い順
- -s r → 総行数の多い順
- -s t → 総実行時間の長い順
mysqldumpslowを実際に使ってみた。
MySQLのスロークエリログが
/tmp/slow.log
分析後のファイルパスが
/tmp/slow-analytics.log
であり、総実行時間を長い順で出力したい時は下記のようなコマンドになります。
mysqldumpslow -s t /tmp/slow.log > /tmp/analytics.log
あとはそれを眺めてEXPLAIN回してINDEX張っていったり、そもそもクエリを見なおしたり、my.cnfの設定を見なおしたりと、地道に早くしていきましょう。