Linux-mysql慢查询日志

一.mysql-慢查询日志:

慢查询日志记录了所有执行时间超过long_query_time设置值并且扫描记录数不小于min_examined_row_limit的所有sql语句的日志。
默认情况下,***管理语句和不使用索引进行的语句***不会记录到慢查询日志中。

二.mysql慢查询配置

慢查询日志功能默认是关闭的。
开启慢查询日志功能:
1.mysql配置文件设置 [修改配置文件my.cnf,在[mysqld]下的下方加入]
slow_query_log=1 #开启慢日志
slow_query_log_file=/data/logs/slow_query_log.txt #慢查询日志存放位置
long_query_time=10 #查询超过十秒就记录
2.mysql命令全局变量设置
#打开慢查询日志功能
mysql> set global slow_query_log=‘ON’;
#设置慢查询日志存放的位置
mysql> set global slow_query_log_file=’/data/logs/slow.log’;
#查询超过10秒就记录
mysql> set global long_query_time=1;
设置完后重启mysql服务:
创建慢查询日志存放目录:#mkdir /data/logs
重启mysql服务:#systemctl restart mysql

三.mysql慢查询日志的读取

慢查询日志记录的格式为纯文本,可以被直接读取。
1.查看设置生效后的参数
#查询超过设置值大小
mysql> show variables like ‘long%’;
±----------------±----------+
| Variable_name | Value |
±----------------±----------+
| long_query_time | 10.000000 |
±----------------±----------+
1 row in set (0.01 sec)
#查询慢查询功能是否开启成功及存放日志路径
mysql> show variables like ‘slow_query%’;
±--------------------±------------------------------+
| Variable_name | Value |
±--------------------±------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /data/logs/slow_query_log.txt |
±--------------------±------------------------------+
2 rows in set (0.00 sec)
2.读取慢查询日志
[root@iZwz96bunw88qiavf6frkbZ admin]# cd /data/logs/
[root@iZwz96bunw88qiavf6frkbZ logs]# ls
slow_query_log.txt
[root@iZwz96bunw88qiavf6frkbZ logs]# ll -h
total 192K
-rw-r–r--. 1 mysql mysql 185K Dec 23 09:35 slow_query_log.txt
[root@iZwz96bunw88qiavf6frkbZ logs]# more slow_query_log.txt
3.如果慢查询日志中记录内容很多,可以使用mysqldumpslow工具来对慢查询日志进行分类汇总。针对于sql文本完全一致只是变量不同的语句,mysqldumpslow将会自动视为同一个语句进行统计,变量值用N代替。
[root@iZwz96bunw88qiavf6frkbZ logs]# mysqldumpslow slow_query_log.txt

Reading mysql slow query log from slow_query_log.txt
Count: 4 Time=748.44s (2993s) Lock=0.00s (0s) Rows=740183.8 (2960735), admin[admin]@4hosts
SELECT * FROM chair_log_back

Count: 5 Time=512.57s (2562s) Lock=0.00s (0s) Rows=517601.8 (2588009), admin[admin]@4hosts
SELECT * FROM chair_log

Count: 4 Time=453.60s (1814s) Lock=0.00s (0s) Rows=1174611.0 (4698444), admin[admin]@4hosts
SELECT * FROM chair_device_log

Count: 3 Time=436.62s (1309s) Lock=0.00s (0s) Rows=248151.0 (744453), admin[admin]@2hosts
SELECT * FROM chair_water_book

Count: 4 Time=431.98s (1727s) Lock=0.00s (0s) Rows=340940.5 (1363762), admin[admin]@3hosts
SELECT * FROM chair_order

Count: 2 Time=319.65s (639s) Lock=0.00s (0s) Rows=219288.0 (438576), admin[admin]@2hosts
SELECT * FROM chair_work_log

Count: 4 Time=271.85s (1087s) Lock=0.00s (0s) Rows=409845.5 (1639382), admin[admin]@4hosts
SELECT * FROM chair_device_statistics

Count: 28 Time=151.93s (4254s) Lock=0.00s (0s) Rows=9.9 (277), root[root]@[127.0.0.1]
select b.,b.total_agent_income-b.store_income as agent_income from
(select
d.id,d.operate_id, d.top_agent_id, d.channel_id, d.store_id, d.device_quantity, d.settlement_type, d.agent_percent,
d.store_percent, d.rental_amount, d.total_charge, round(d.wechat_fee,N) AS wechat_fee, round(d.alipay_fee,N) AS alipay_fee, round(d.total_income,N) AS total_income,
round(d.platform_income,N) AS platform_income, round(d.total_agent_income,N) AS total_agent_income, round(d.store_income_rent,N) AS store_income_rent, round(d.store_income_divide,N) AS store_income_divide, d.divide_date,
d.create_time,ut.name AS top_agent_name ,u.name AS agent_name ,ch.name AS channel_name,s.name AS store_name,st.name as type_name,
CASE d.settlement_type
WHEN N THEN
round(d.store_income_rent,N)
WHEN N THEN
round(d.store_income_divide,N)
WHEN N THEN
if(d.store_income_rent>=d.store_income_divide,round(d.store_income_rent,N),round(d.store_income_divide,N))
END AS store_income,
CASE d.settlement_type
WHEN N THEN
round(d.store_income_rent,N)
WHEN N THEN
round(d.store_income_divide,N)
WHEN N THEN
if(d.store_income_rent >= d.store_income_divide,CONCAT( round(d.store_income_rent,N),‘S’,round(d.store_income_divide,N),‘S’, round(d.store_income_rent,N)),CONCAT( round(d.store_income_rent,N),‘S’,round(d.store_income_divide,N),‘S’,round(d.store_income_divide,N)))
END AS store_income_description
from chair_divided_store d
LEFT JOIN chair_user ut ON d.top_agent_id=ut.id
LEFT JOIN chair_user u ON d.agent_id=u.id
LEFT JOIN chair_channel ch ON d.channel_id=ch.id
LEFT JOIN chair_store s ON d.store_id=s.id
LEFT JOIN chair_store_type st ON s.type_id=st.id
WHERE s.merchant_id = ‘S’
and d.divide_date >= ‘S’
ORDER BY d.agent_id,d.divide_date desc ,d.total_charge desc,d.channel_id ,d.store_id
LIMIT N,N
) b
**…***
注:mysql慢查询日志对于应用中性能问题的SQL很有帮助,建议打开此功能,经常查看分析,不断优化SQL,提升应用性能。

你可能感兴趣的:(Mysql_learning)