MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中。long_query_time的默认值为10,意思是运行10S以上的语句。默认情况下,Mysql数据库并不启动慢查询日志,需要我们手动来设置这个参数,当然,如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响。慢查询日志支持将日志记录写入文件,也支持将日志记录写入数据库表。
查看/开启慢查询记录功能
//查看慢查询功能是否开启
mysql> show variables like "slow%";
+---------------------+------------------------------------------------------------+
| Variable_name | Value |
+---------------------+------------------------------------------------------------+
| slow_launch_time | 2 |
| slow_query_log | OFF(OFF——>关闭、ON——>开启) |
| slow_query_log_file | D:\mysql\mysql-5.7.26-winx64\data\DESKTOP-KL3FR80-slow.log |
+---------------------+------------------------------------------------------------+
3 rows in set, 1 warning (0.00 sec)
//开启设置慢查询,设置全局变量slow_query_log状态
mysql> set global slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)
结果:
mysql> show variables like "slow%";
+---------------------+------------------------------------------------------------+
| Variable_name | Value |
+---------------------+------------------------------------------------------------+
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | D:\mysql\mysql-5.7.26-winx64\data\DESKTOP-KL3FR80-slow.log |
+---------------------+------------------------------------------------------------+
查看/设置“慢查询”的时间定义
//查看慢查询默认的时间定义
mysql> show variables like 'long%';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set, 1 warning (0.01 sec)
如上所述语句含义慢查询定义的时间为1.000000秒。
//设置慢查询的时间
mysql> set long_query_time=0.00001;
Query OK, 0 rows affected (0.00 sec)
查看慢查询日志存放位置
mysql> show variables like "%query%";
+------------------------------+------------------------------------------------------------+
| Variable_name | Value |
+------------------------------+------------------------------------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| long_query_time | 0.000010 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | D:\mysql\mysql-5.7.26-winx64\data\DESKTOP-KL3FR80-slow.log |
+------------------------------+------------------------------------------------------------+
13 rows in set, 1 warning (0.00 sec)
//slow_query_log_file参数为慢查询日志存放位置
查看/开启慢查询日志
//慢查询日志相关参数
slow_query_log :是否开启慢查询日志,1表示开启,0表示关闭。
//查看慢查询日志是否开启
mysql> show variables like "%slow_query_log%";
+---------------------+------------------------------------------------------------+
| Variable_name | Value |
+---------------------+------------------------------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | D:\mysql\mysql-5.7.26-winx64\data\DESKTOP-KL3FR80-slow.log |
+---------------------+------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
//开启慢查询日志
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
结果:
mysql> show variables like "%slow_query_log%";
+---------------------+------------------------------------------------------------+
| Variable_name | Value |
+---------------------+------------------------------------------------------------+
| slow_query_log | ON |
| slow_query_log_file | D:\mysql\mysql-5.7.26-winx64\data\DESKTOP-KL3FR80-slow.log |
+---------------------+------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
参考:(如下连接为其他博主辛苦总结,可参考学习)
http://www.cnblogs.com/kimi9py/p/5735043.html
日志分析:
https://blog.csdn.net/timchen525/article/details/75268151
参考:开启慢查询 分析日志