写文章

MySQL 慢查询日志

2018-11-30 01:36:37

2059 | 0 | 0


MySQL有一种日志,叫做慢查询日志,主要就是用来记录一些耗时的查询操

 

作。通过这个日志我们就可以分析出哪些的操作是影响性能的,我们需要对其

 

进行一些优化措施。

 

视频链接:http://www.roncoo.com/course/view/658088f6e77541f5835b61800314083e

 

 

查看开启状态

 

 images/2D7fak3NTWy7aaGCDGYWXzbhm53Rck8H.png

 

上面的截图是我在 windows 下安装的 MySQL5.7 版本,我们可以发现,这个版本是开启了慢查询的。我在 CentOS6.9 下采用 yum 的方式安装的 MySQL5.7 默认没有开启慢查询日志。不管默认有没有给我们开启,我们是需要了解慢查询日志是如何开启的,开启的方式也非常简单。找到 MySQL 的配置文件,Windows 下是 my.ini,Linux 下的是 my.cnf。进行如下配置就可以了。

 

slow-query-log=1

 

 

slow_query_log_file="mysql-slow.log"

 

 

long_query_time=10

 

第一行是指定开启慢查询日志

 

第二行是指定慢查询日志的路径

 

第三行是指定查询时间大于多少的才进行记录,但是是毫秒,也就是操作大于 10ms 的操作都会被记录。

 

配置完毕之后要重启 MySQL 生效。

 

下面来看看慢查询日志的内容


C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqld.exe, Version: 5.7.16-log (MySQL
Community Server (GPL)). started with:
TCP Port: 3306, Named Pipe: (null)
TimeId CommandArgument
#Time: 2017-07-07T06:35:46.995201Z
#User@Host: root[root] @ localhost [::1]  Id:10
#Query_time: 12.522116 Lock_time: 0.000501 Rows_sent: 0 Rows_examined: 483968 use test;
SET timestamp=1499409346;
insert into test (id,name) (select uuid() id,name from test);
#Time: 2017-07-07T06:36:15.258316Z
#User@Host: root[root] @ localhost [::1]  Id:10
#Query_time: 24.543267 Lock_time: 0.000501 Rows_sent: 0 Rows_examined: 967936 SET timestamp=1499409375;
insert into test (id,name) (select uuid() id,name from test);
#Time: 2017-07-07T06:37:15.021922Z
#User@Host: root[root] @ localhost [::1]  Id:10
#Query_time: 56.283040 Lock_time: 0.000499 Rows_sent: 0 Rows_examined: 1935872 SET timestamp=1499409435;
insert into test (id,name) (select uuid() id,name from test);
#Time: 2017-07-07T06:40:07.866659Z
#User@Host: root[root] @ localhost [::1]  Id:10
#Query_time: 133.866927 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 3871744 SET timestamp=1499409607;
insert into test (id,name) (select uuid() id,name from test);


我们可以看到在 2017-07-07 日,有多个慢查询产生。单独抽取一组,如下


#Time: 2017-07-07T06:35:46.995201Z
#User@Host: root[root] @ localhost [::1]  Id:10
#Query_time: 12.522116 Lock_time: 0.000501 Rows_sent: 0 Rows_examined: 483968 use test;
SET timestamp=1499409346;
insert into test (id,name) (select uuid() id,name from test);


Query_time 表示的是耗时时间


下面是一些操作,这的主要操作就是一个 insert


这就是慢查询日志。

0

收藏
分享