文章来源: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`

文章来源:https://www.cnblogs.com/hello-tl/p/9229676.html

05-04 03:20