文章來源:https://www.cnblogs.com/hello-tl/p/9229676.html
1.使用Mysql慢查詢日志配置
查看慢查詢日志是否開啟 OFF關閉 ON開啟 show variables like 'slow_query_log' 開啟慢查詢日志 set global slow_query_log = on 慢查詢日志所存儲的文件位置 show variables like 'slow_query_log_file' 設置慢查詢日志位置 set global slow_query_log_file = '/var/lib/mysql/iZbp1akzlq26t30fbsdwh7Z-slow.log' 是否吧沒有使用索引的sql記錄到慢查詢日志當中 PFF關閉 ON開啟 show variables like 'log_queries_not_using_indexes' 設置吧沒有使用索引的sql記錄添加到慢查詢日志當中 set global log_queries_not_using_indexes = on; 查看long_query_time設置的時間 show variables like 'long_query_time'; 超過多少秒之后的查詢記錄到慢查詢日志當中 set [session|global] long_query_time = 1
2.慢查詢日志存儲格式
-- 執行sql時間 # Time: 2018-06-26T07:23:35.892991Z -- 執行SQL的主機信息 # User@Host: root[root] @ localhost [] Id: 387088 -- SQL的執行信息 # Query_time: 0.000155 Lock_time: 0.000057 Rows_sent: 12 Rows_examined: 31 -- SQL執行時間 SET timestamp=1529997815; -- SQL內容 SELECT `key`,`value` FROM `yp_setti
3.慢查詢日志的分析工具 mysqldumpslow
[root@iZbp1akzlq26t30fbsdwh7Z mysql] # mysqldumpslow -h
Option h requires an argument
ERROR: bad option
# 最簡單的使用方式 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參數 通過什么方式來排序
-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 查看前多少條日志
-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
4.mysqldumpslow 分析結果
[root@iZbp1akzlq26t30fbsdwh7Z mysql]# mysqldumpslow /var/lib/mysql/iZbp1akzlq26t30fbsdwh7Z-slow.log -t 2 -s al Reading mysql slow query log from /var/lib/mysql/iZbp1akzlq26t30fbsdwh7Z-slow.log -- count 執行次數 -- time 執行時間 -- lock 鎖定時間 -- rows 返回條數 -- root[root]@localhost 通過那個用戶執行的 Count: 5 Time=0.00s (0s) Lock=0.01s (0s) Rows=5.0 (25), root[root]@localhost SHOW COLUMNS FROM `yp_area`
