码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 慢查询日志

时间:2017-09-07 11:59:25      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:www   记录   test   多个   重启   version   set   需要   width   

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

 

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

 

进行一些优化措施。

  

查看开启状态

技术分享

 

上面的截图是我在 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 生效。

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

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

 

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

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

Query_time 表示的是耗时时间

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

这就是慢查询日志。

MySQL 慢查询日志

标签:www   记录   test   多个   重启   version   set   需要   width   

原文地址:http://www.cnblogs.com/roncoo/p/7488598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!