mysql慢查询
1、慢查询的作用
慢查询可以用于记录一些查询较慢的SQL语句,帮助我们后面分析问题所在,并为我们的SQL提供优化来节约资源,但是开启慢查询或多或少会带来一定的性能影响。
2、慢查询的相关参数
slow_query_log:是否开启慢查询日志,1表示开启,0表示关闭
slow_query_log_file:指定慢查询日志的存放路径
long_query_time:开启慢查询的阈值,默认情况下为10s
log_output:慢查询的日志存储方式,通常有两种方式,file和table,存储方式为文件则性能更高
log_queries_not_using_indexes:未使用索引的查询也被记录到慢查询日志
3、开启方式
注:开启方式有两种(当下 和永久),对于永久开启慢查询的方式,直接修改/etc/my.cf文件即可
3.1 slow_query_log
默认情况下slow_query_log
的值为OFF
,表示慢查询日志是禁用的,可以通过设置slow_query_log
的值来开启
mysql> show variables like '%slow_query_log%'; +---------------------+-----------------------------------------------+ | Variable_name | Value | +---------------------+-----------------------------------------------+ | slow_query_log | OFF | | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log | +---------------------+-----------------------------------------------+ 2 rows in set (0.00 sec) mysql> set global slow_query_log=1; Query OK, 0 rows affected (0.09 sec)
3.2 slow_query_log_file
这个参数用于指定慢查询日志的存放路径
mysql> show variables like 'slow_query_log_file'; +---------------------+-----------------------------------------------+ | Variable_name | Value | +---------------------+-----------------------------------------------+ | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log | +---------------------+-----------------------------------------------+ 1 row in set (0.00 sec)
3.3 slow_query_time
指定慢查询的阈值,默认为10s,修改之后时间显示未变,用 show global variables like 'long_query_time' 查询
mysql> show variables like 'long_query_time%'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set (0.00 sec) mysql> set global long_query_time=4; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+
3.4 log_output
指定日志的存储方式
mysql> show variables like '%log_output%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ 1 row in set (0.00 sec) mysql> set global log_output='TABLE'; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%log_output%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | TABLE | +---------------+-------+ 1 row in set (0.00 sec)
试验查询
mysql> select sleep(5) ; +----------+ | sleep(5) | +----------+ | 0 | +----------+ 1 row in set (5.00 sec)
3.5 log_queries_not_using_indexes
该系统变量指定未使用索引的查询也被记录到慢查询日志中
mysql> show variables like 'log_queries_not_using_indexes'; +-------------------------------+-------+ | Variable_name | Value | +-------------------------------+-------+ | log_queries_not_using_indexes | OFF | +-------------------------------+-------+ 1 row in set (0.00 sec) mysql> set global log_queries_not_using_indexes=1; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'log_queries_not_using_indexes'; +-------------------------------+-------+ | Variable_name | Value | +-------------------------------+-------+ | log_queries_not_using_indexes | ON | +-------------------------------+-------+ 1 row in set (0.00 sec)