MySql慢查询日志——开启/查看/删除

1,开启慢查询日志

修改mysql.ini文件,加入如下配置:

[mysqld]
log-slow-queries=H:\mysql_log\slow_query.log
long-query-time=3
set global slow_query_log=ON;

set global slow_launch_time=5;

show variables like "%slow%";

设置查询日志的存储位置,慢查询时间。

show variables like '%slow%'; /*查看慢查询配置*/

show variables like "long_query_time"; /*查看慢查询时间*/

show status like "%slow_queries%";/*查看慢查询配置情况*/

2,查看慢查询

打开log文件:

F:\mysql\bin\mysqld, Version: 5.5.24-log (MySQL Community Server (GPL)). started with:
TCP Port: 3306, Named Pipe: (null)
Time                 Id Command    Argument
# Time: 161008 21:20:02
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 8.718499  Lock_time: 0.001002 Rows_sent: 0  Rows_examined: 908972
use energy;
SET timestamp=1475932802;
/*结束时间*/

select 

DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') as create_time,/*创建时间*/
(	select count(0)
		from biz_work_order tmp
		where
		DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d')
		or (tmp.create_time<DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status` in (1,2,4))
	)as should_change_worker_order

from biz_work_order worker_order 

WHERE
				(CASE  WHEN IFNULL(@belongCity,'0')='0' OR @belongCity=''  THEN  1 ELSE   

        CAST(LOCATE(@belongCity,worker_order.belong_city) AS SIGNED )   

        END)=1
AND

	(CASE  WHEN IFNULL(@energyStation,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@energyStation,worker_order.repository_id) AS SIGNED )   

        END)=1
AND
	(CASE  WHEN IFNULL(@serviceStatioin,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@serviceStatioin,worker_order.station_id) AS SIGNED )   

        END)=1
AND
				DATE_FORMAT(worker_order.create_time, '%Y-%m-%d') BETWEEN DATE_FORMAT(cast(@beginDateTime as datetime), '%Y-%m-%d') and DATE_FORMAT(cast(@endDateTime as datetime), '%Y-%m-%d')

GROUP BY DATE_FORMAT(worker_order.create_time,'%Y-%m-%d');
# Time: 161008 21:20:40
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 33.695952  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 3239812
SET timestamp=1475932840;
/*结束时间*/

select 

DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') as create_time,/*创建时间*/
(	select count(0)
		from biz_work_order tmp
		where
		DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d')
		or (tmp.create_time<DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status` in (1,2,4))
	)as should_change_worker_order

from biz_work_order worker_order 

WHERE
				(CASE  WHEN IFNULL(@belongCity,'0')='0' OR @belongCity=''  THEN  1 ELSE   

        CAST(LOCATE(@belongCity,worker_order.belong_city) AS SIGNED )   

        END)=1
AND

	(CASE  WHEN IFNULL(@energyStation,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@energyStation,worker_order.repository_id) AS SIGNED )   

        END)=1
AND
	(CASE  WHEN IFNULL(@serviceStatioin,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@serviceStatioin,worker_order.station_id) AS SIGNED )   

        END)=1
AND
				DATE_FORMAT(worker_order.create_time, '%Y-%m-%d') BETWEEN DATE_FORMAT(cast(@beginDateTime as datetime), '%Y-%m-%d') and DATE_FORMAT(cast(@endDateTime as datetime), '%Y-%m-%d')

GROUP BY DATE_FORMAT(worker_order.create_time,'%Y-%m-%d');
# Time: 161008 21:21:49
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 48.615826  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 4920668
SET timestamp=1475932909;
/*结束时间*/

select 

DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') as create_time,/*创建时间*/
(	select count(0)
		from biz_work_order tmp
		where
		DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d')
		or (tmp.create_time<DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status` in (1,2,4))
	)as should_change_worker_order

from biz_work_order worker_order 

WHERE
				(CASE  WHEN IFNULL(@belongCity,'0')='0' OR @belongCity=''  THEN  1 ELSE   

        CAST(LOCATE(@belongCity,worker_order.belong_city) AS SIGNED )   

        END)=1
AND

	(CASE  WHEN IFNULL(@energyStation,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@energyStation,worker_order.repository_id) AS SIGNED )   

        END)=1
AND
	(CASE  WHEN IFNULL(@serviceStatioin,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@serviceStatioin,worker_order.station_id) AS SIGNED )   

        END)=1
AND
				DATE_FORMAT(worker_order.create_time, '%Y-%m-%d') BETWEEN DATE_FORMAT(cast(@beginDateTime as datetime), '%Y-%m-%d') and DATE_FORMAT(cast(@endDateTime as datetime), '%Y-%m-%d')

GROUP BY DATE_FORMAT(worker_order.create_time,'%Y-%m-%d');
# Time: 161008 21:22:37
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 41.309375  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 6050268
SET timestamp=1475932957;
call Pro_query_work_order_report('50010',NULL,270,date_sub(curdate(), INTERVAL 6 DAY),curdate());
# Time: 161008 21:23:59
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 79.538506  Lock_time: 0.000000 Rows_sent: 7  Rows_examined: 12282968
SET timestamp=1475933039;
call Pro_query_work_order_report('50010',NULL,270,date_sub(curdate(), INTERVAL 6 DAY),curdate());

被自己写的SQL慢哭了。。。

三,删除慢查询日志

直接delete掉log,删除后需要执行下面两句中的任意一句,重新生成日志文件:

  -> mysqladmin -u root-p flush-logs
    -> flush logs
时间: 2024-11-24 07:40:47

MySql慢查询日志——开启/查看/删除的相关文章

MySQL慢查询日志开启分析详解介绍

(1)配置开启 Linux: 在mysql配置文件 my.cnf 中增加如下语句: log-slow-queries=/var/lib/mysql/slowquery.log #指定日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log long_query_time=5 #记录超过的时间,默认为10s,这里设置为查询时间超过5s的查询语句 log-queries-not-using-indexes = on # 列出没有使用索引的查询语句 #log-querie

mysql 慢查询日志开启与使用详解

MySQL慢日志往往是大家定位SQL性能问题.通过慢日志分析可以调整索引使用.这是使用最多的一个功能. 目前MySQL也支持毫秒级别以下的慢日志记录,这个功能其实有很多用处,这里大概给大家讲解一下.该功能开启参数:long_query_time 查看慢查询是否开启:  代码如下 复制代码 mysql> show variables like '%slow%'; +---------------------+-------------------------+ | Variable_name   

【转载】MySQL慢查询日志总结

MySQL慢查询日志总结 2016-06-17 10:32 by 潇湘隐者, 1166 阅读, 1 评论, 收藏, 编辑 慢查询日志概念      MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中.long_query_time的默认值为10,意思是运行10S以上的语句.默认情况下,Mysql数据库并不启动慢查询日志,需要我们手动来设置这个参数,当然,如果不是

mysql慢查询日志分析实例

1.MySQL 慢查询日志分析 pt-query-digest分析慢查询日志 pt-query-digest –report slow.log 报告最近半个小时的慢查询: pt-query-digest –report –since 1800s slow.log 报告一个时间段的慢查询: pt-query-digest –report –since '2013-02-10 21:48:59′ –until '2013-02-16 02:33:50′ slow.log 报告只含select语句的慢

对MySQL慢查询日志进行分析的基本教程_Mysql

0.首先查看当前是否开启慢查询: (1)快速办法,运行sql语句 show VARIABLES like "%slow%" (2)直接去my.conf中查看. my.conf中的配置(放在[mysqld]下的下方加入) [mysqld] log-slow-queries = /usr/local/mysql/var/slowquery.log long_query_time = 1 #单位是秒 log-queries-not-using-indexes 使用sql语句来修改:不能按照m

MySQL 慢查询日志分析及可视化结果

MySQL 慢查询日志分析及可视化结果 MySQL 慢查询日志分析 pt-query-digest分析慢查询日志 pt-query-digest --report slow.log 报告最近半个小时的慢查询: pt-query-digest --report --since 1800s slow.log 报告一个时间段的慢查询: pt-query-digest --report --since '2013-02-10 21:48:59' --until '2013-02-16 02:33:50'

ELK logstash 处理MySQL慢查询日志的示例

在生产环境下,logstash 经常会遇到处理多种格式的日志,不同的日志格式,解析方法不同.下面来说说logstash处理多行日志的例子,对MySQL慢查询日志进行分析,这个经常遇到过,网络上疑问也很多. MySQL慢查询日志格式如下: # User@Host: ttlsa[ttlsa] @  [10.4.10.12]  Id: 69641319 # Query_time: 0.000148  Lock_time: 0.000023 Rows_sent: 0  Rows_examined: 20

MySQL慢查询日志

链接 启动与停止 开启慢查询日志 分析慢查询日志 1 链接 个人博客: alex-my.xyz CSDN: blog.csdn.net/alex_my 2 启动与停止 mac下mysql的操作 启动 sudo /usr/local/mysql/support-files/mysql.server start 停止 sudo /usr/local/mysql/support-files/mysql.server stop 重启 sudo /usr/local/mysql/support-files

MySQL慢查询日志总结

慢查询日志概念      MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中.long_query_time的默认值为10,意思是运行10S以上的语句.默认情况下,Mysql数据库并不启动慢查询日志,需要我们手动来设置这个参数,当然,如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响.慢查询日志支持将日志记录写入文件,也支