前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL 慢查询日志

MySQL 慢查询日志

原创
作者头像
吴生
修改2017-09-08 16:23:27
2K0
修改2017-09-08 16:23:27
举报
文章被收录于专栏:吴生的专栏吴生的专栏

MySQL有一种日志,叫做慢查询日志,主要就是用来记录一些耗时的查询操作。通过这个日志我们就可以分析出哪些的操作是影响性能的,我们需要对其进行一些优化措施。

查看开启状态

[img59b24a7e6cd5c.png]
[img59b24a7e6cd5c.png]

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

代码语言:txt
复制
slow-query-log=1
 
 
slow_query_log_file="mysql-slow.log"
 
 
long_query_time=10

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

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

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

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

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

代码语言:txt
复制
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 日,有多个慢查询产生。单独抽取一组,如下

代码语言:txt
复制
#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

这就是慢查询日志。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档