0%

mysql【数据库优化】

mysql慢查询

查看mysql慢查询日志

-- 慢查询日志
show variables like '%slow_query%';
+---------------------+--------------------------------------+
| Variable_name       | Value                                |
+---------------------+--------------------------------------+
| slow_query_log      | OFF                                  |
| slow_query_log_file | /var/lib/mysql/7b32a384231d-slow.log |
+---------------------+--------------------------------------+
2 rows in set

-- 不使用索引查询日志
mysql> show variables like '%log_que%';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF   |
+-------------------------------+-------+
1 row in set

-- 查询时间设置,查询超过多少秒才记录
mysql> show variables like '%long_que%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set

开启慢查询日志

-- 方法一
set global  slow_query_log=ON;
set global  log_queries_not_using_indexes=ON;

-- 方法二,修改配置文件my.cnf
slow_query_log = ON
slow_query_log_file = /usr/local/mysql/data/slow.log
long_query_time = 1
-- 方法二需要重启
service mysqld restart

慢查询分析工具

mysqldumpslow

pt-query-digest