如何在MySQL中使用慢查询日志

22次阅读
没有评论

共计 3656 个字符,预计需要花费 10 分钟才能阅读完成。

自动写代码机器人,免费开通

这期内容当中丸趣 TV 小编将会给大家带来有关如何在 MySQL 中使用慢查询日志,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

慢查询日志相关参数

MySQL 慢查询的相关参数解释:slow_query_log:是否开启慢查询日志,1 表示开启,0 表示关闭。

slow_query_log:是否开启慢查询日志,1 表示开启,0 表示关闭。

log-slow-queries:旧版(5.6 以下版本)MySQL 数据库慢查询日志存储路径。可以不设置该参数,系统则会默认给一个缺省的文件 host_name-slow.log

slow-query-log-file:新版(5.6 及以上版本)MySQL 数据库慢查询日志存储路径。可以不设置该参数,系统则会默认给一个缺省的文件 host_name-slow.log

long_query_time:慢查询阈值,当查询时间多于设定的阈值时,记录日志。

log_queries_not_using_indexes:未使用索引的查询也被记录到慢查询日志中(可选项)。

log_output:日志存储方式。log_output= FILE 表示将日志存入文件,默认值是 FILE。log_output= TABLE 表示将日志存入数据库,这样日志信息就会被写入到 mysql.slow_log 表中。MySQL 数据 br 库支持同时两种日志存储方式,配置的时候以逗号隔开即可,如:log_output= FILE,TABLE。日志记录到系统的专用日志表中,要比记录到文件耗费更多的系统资源,因此对于需要启用慢查询日志,又需 br 要能够获得更高的系统性能,那么建议优先记录到文件。

一. 设置方法

使用慢查询日志里捕获

启用之前需要先进行一些设置

方法一:全局变量设置

设置慢查询日志的日志文件位置

set global slow_query_log_file =  D:/slow_log/slow_log.log  ;

设置是否对未使用索引的 SQL 进行记录

set global log_queries_not_using_indexes = on;

设置只要 SQL 执行时间超过 n 秒的就记录

set global long_query_time = 0.001 ;

此处设置的 0.001 秒,便于测试,一般情况比这个大

启用 mysql 慢查询日志

set global slow_query_log = on;

方法二:配置文件设置

修改配置文件 my.cnf,在 [mysqld] 下的下方加入

[mysqld]
slow_query_log = ON
log_queries_not_using_indexes = ON;
slow_query_log_file = /usr/local/mysql/data/slow.log
long_query_time = 1

查看设置后的参数

show variables like  slow_query% 
show variables like  long_query__time

二. 慢查询日志记录的内容

Time Id Command Argument
# Time: 2019-01-08T04:12:09.269315Z 
# User@Host: h6_test[h6_test] @ localhost [::1] Id: 12 
# Query_time: 0.000831 Lock_time: 0.000198 Rows_sent: 1 Rows_examined: 3 
use mc_productdb;
SET timestamp=1546920729;
SELECT t.customer_id,t.title,t.content 
FROM (SELECT customer_id FROM product_comment WHERE product_id =199726 AND audit_status = 1 LIMIT 0,15)a JOIN product_comment t 
ON a.customer_id = t.comment_id;

Time:执行查询的日期时间

User@Host:执行查询的用户和客户端 IP

Id:是执行查询的线程 Id

Query_time:SQL 执行所消耗的时间

Lock_time:执行查询对记录锁定的时间

Rows_sent:查询返回的行数

Rows_examined:为了返回查询的数据所读取的行数

三. 如何分析慢查询日志

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

由于慢查询日志中会含有大量的重复的 SQL, 为了方便,可以通过 mysql 提供的命令行工具 mysqldumpslow 来分析日志

$ mysqldumpslow.pl slow_log.log
Reading mysql slow query log from slow_log.log
Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts
 C:\Program Files\MySQL\MySQL Server N.N\bin\mysqld.exe, Version: N.N.N-log (MySQL Community Server (GPL)). started with:
 TCP Port: N, Named Pipe: MySQL
 # Time: N-N-08T04:N:N.269315Z
 # User@Host: h6_test[h6_test] @ localhost [::N] Id: N
 # Query_time: N.N Lock_time: N.N Rows_sent: N Rows_examined: N
 use mc_productdb;
 SET timestamp=N;
 SELECT t.customer_id,t.title,t.content
 FROM (
 SELECT customer_id FROM product_comment WHERE product_id =N AND audit_status = N LIMIT N,N
 )a JOIN product_comment t
 ON a.customer_id = t.comment_id

与慢查询日志中记录的数据是相似的,只是多出了一行 Count,这一行记录的是这条 SQL 在记录慢查询日志期间的执行次数,如果一个 SQL 多次被执行,用这个命令分析时,只会出现一个 SQL 日志,Count 里的数值代表执行次数,其他数字为了合并表示用 N 代替

上述就是丸趣 TV 小编为大家分享的如何在 MySQL 中使用慢查询日志了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注丸趣 TV 行业资讯频道。

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计3656字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)