MySQL自帶slow log的分析工具mysqldumpslow,但是沒有說明。本文通過分析該腳本,介紹了其用法。slow log是MySQL根據(jù)SQL語句的執(zhí)行時間設定,寫入的一個文件,用于分析執(zhí)行較慢的語句。
只要在 my.cnf 文件中配置好:
log-slow-queries = [slow_query_log_filename]
即可記錄超過默認的 10s 執(zhí)行時間的 SQL 語句。
如果要修改默認設置,可以添加:
long_query_time = 5
設定為 5s 。
如果要記錄所有 SQL 語句,可以寫入:
log-long-format
# t=time, l=lock time, r=rows
# at, al, 以及 ar 是對應的平均值
mysqldumpslow 可以接受的參數(shù)有:
‘v+‘, # verbose
‘d+‘, # debug
‘s=s‘, # 排序 (t, at, l, al, r, ar etc)
‘r!‘, # 倒排序 (largest last instead of first)
‘t=i‘, # 顯示最高的 n 個查詢
‘a(chǎn)!‘, # 不把所有的數(shù)字以 N ,字符串以 ‘S‘ 顯示
‘n=i‘, # abstract numbers with at least n digits within names
‘g=s‘, # grep: only consider stmts that include this string
‘h=s‘, # hostname of db server for *-slow.log filename (can be wildcard)
‘i=s‘, # name of server instance (if using mysql.server startup script)
‘l!‘, # don‘t subtract lock time from total time