【Redis】【MySQL】redis与mysql的慢查询

redis和mysql都存在对于慢查询的日志记录,下面将叙述一下两者的慢查询。

一,redis[1]

redis的慢查询日志本质上是一个list对象,不过redis并没有提供慢查询日志的key。开发者可以通过下列命令查询慢查询日志:

#获得慢查询日志,n可以指定条数
slowlog get [n]
#慢查询日志列表当前的长度
slowlog len
#慢查询日志重置
slowlog reset

redis提供了slowlog-log-slower-tan和slowlog-max-len配置,slowlog-log-slower-than是慢查询日志的阈值,单位是微秒,默认值是10000;slowlog-max-len是慢查询日志最多存储多少条。

我们可以修改配置文件来修改上述配置,也可以通过在客户端动态修改,如

#修改配置,重启后失效
config set slowlog-max-len 1000
#将配置持久化到配置文件
config rewrite

redis的慢查询日志本质上是一个队列或者说列表,有固定的长度限制,如果超出长度,会丢失一部分慢查询日志。如果想要将慢查询日志持久化,可以通过定时任务将慢查询日志的结果持久化到其他存储中[1]。

二,mysql

进入mysql客户端,

查询是否开启慢查询日志:

#slow_query_log,是否开启了慢查询日志,当前状态为OFF
mysql> show variables like 'slow_query_log'
    -> ;
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | OFF   |
+----------------+-------+
1 row in set (0.01 sec)

#开启慢查询日志
mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | ON    |
+----------------+-------+
1 row in set (0.00 sec)

慢查询日志阈值,单位为秒,默认10秒

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

慢查询日志路径

mysql> show variables like 'slow_query_log_file';
+---------------------+-----------------------------------+
| Variable_name       | Value                             |
+---------------------+-----------------------------------+
| slow_query_log_file | /var/lib/mysql/localhost-slow.log |
+---------------------+-----------------------------------+
1 row in set (0.00 sec)


是否在慢查询日志中记录没有使用索引的SQL

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)

这里为了演示,我们把慢查询日志的阈值调为0s,这样任何sql都会被判断为慢查询并记录在日志中。

mysql> set global long_query_time=0;
Query OK, 0 rows affected (0.00 sec)

我们开启另外一个shell,去查看慢查询日志,可以看到此时慢查询日志多了一些日志

#没有开启慢查询日志时,日志文件中没有记录
[root@localhost usr]# cat /var/lib/mysql/localhost-slow.log 
/usr/sbin/mysqld, Version: 5.7.44 (MySQL Community Server (GPL)). started with:
Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
Time                 Id Command    Argument
[root@localhost usr]# vi /var/lib/mysql/localhost-slow.log ;
#开启慢查询日志后,日志中有了记录
[root@localhost usr]# cat /var/lib/mysql/localhost-slow.log 
/usr/sbin/mysqld, Version: 5.7.44 (MySQL Community Server (GPL)). started with:
Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
Time                 Id Command    Argument
# Time: 2023-12-15T12:08:27.357678Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000328  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1702642107;
select @@version_comment limit 1;
# Time: 2023-12-15T12:09:02.503218Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.003156  Lock_time: 0.000522 Rows_sent: 1  Rows_examined: 1052
SET timestamp=1702642142;
show variables like 'slow_query_log_file';
# Time: 2023-12-15T12:10:19.797352Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000141  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642219;
set global slow_query_log = on;
# Time: 2023-12-15T12:10:33.596783Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.001013  Lock_time: 0.000140 Rows_sent: 1  Rows_examined: 1052
SET timestamp=1702642233;
show variables like 'slow_query_log';
# Time: 2023-12-15T12:10:59.163838Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000086  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642259;
set global long_query_time = 0;
# Time: 2023-12-15T12:11:08.379448Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000115  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642268;
select * from emp;
# Time: 2023-12-15T12:11:23.468288Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000652  Lock_time: 0.000120 Rows_sent: 5  Rows_examined: 5
SET timestamp=1702642283;
show databases;
# Time: 2023-12-15T12:11:27.571064Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000131  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1702642287;
SELECT DATABASE();
# Time: 2023-12-15T12:11:27.571279Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000039  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
use locktest;
SET timestamp=1702642287;
# administrator command: Init DB;
# Time: 2023-12-15T12:11:27.573106Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000214  Lock_time: 0.000075 Rows_sent: 5  Rows_examined: 5
SET timestamp=1702642287;
show databases;
# Time: 2023-12-15T12:11:27.573360Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000081  Lock_time: 0.000029 Rows_sent: 4  Rows_examined: 4
SET timestamp=1702642287;
show tables;
# Time: 2023-12-15T12:11:27.573615Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000143  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642287;
;
# Time: 2023-12-15T12:11:27.573756Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000068  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642287;
;
# Time: 2023-12-15T12:11:27.573919Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000044  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642287;
;
# Time: 2023-12-15T12:11:27.573998Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000035  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1702642287;
;
# Time: 2023-12-15T12:11:32.483758Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000207  Lock_time: 0.000108 Rows_sent: 4  Rows_examined: 4
SET timestamp=1702642292;
show tables;
# Time: 2023-12-15T12:11:37.555298Z
# User@Host: root[root] @ localhost []  Id:     6
# Query_time: 0.000271  Lock_time: 0.000078 Rows_sent: 12  Rows_examined: 12
SET timestamp=1702642297;
select * from emp;

其中Query_time为查询时间,Lock_time为等待锁的阻塞时间,加起来是在Mysql中查询使用的时间。

参考文章:
[1],redis开发与运维,ISBN 978-7-111-55797-5

你可能感兴趣的:(mysql,Redis,redis,mysql,数据库)