欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql慢查询操作实例分析【开启、测试、确认等】

程序员文章站 2023-11-02 11:24:28
本文实例讲述了mysql慢查询操作。分享给大家供大家参考,具体如下: mysql有些sql会执行很慢,有可能造成服务器负载飙升 首先查询 确定影响负载的是mysql ,使用top命...

本文实例讲述了mysql慢查询操作。分享给大家供大家参考,具体如下:

mysql有些sql会执行很慢,有可能造成服务器负载飙升

首先查询 确定影响负载的是mysql ,使用top命令,ps命令等

其次,进入mysql,使用show full processlist查询执行中的sql语句,看看问题,使用explain 命令 查看状态

最后找出sql语句杀死或者优化

centos7上面安装mariadb服务

yum -y install mariadb-server mariadb-devel

开启慢查询

more /etc/my.cnf.d/server.cnf
[mariadb]
slow_query_log=on
slow_query_log_file=/usr/local/mysql/data/slow.log
long_query_time=1

启动mariadb服务

systemctl start mariadb

查询mysql的慢查询是否开启,以及多久的时间以上是慢查询

mariadb [(none)]> show variables like '%slow_query%';
+---------------------+--------------------------------+
| variable_name    | value             |
+---------------------+--------------------------------+
| slow_query_log   | on               |
| slow_query_log_file | /usr/local/mysql/data/slow.log |
+---------------------+--------------------------------+
2 rows in set (0.00 sec)

mariadb [(none)]> show variables like 'long_query_time';
+-----------------+----------+
| variable_name  | value  |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)

#如果没用开启慢查询,可以在命令行开启
mysql> set global slow_query_log=1;
query ok, 0 rows affected (0.00 sec)

 测试慢查询,以及查看日志

mariadb [(none)]> select sleep(2);
+----------+
| sleep(2) |
+----------+
|    0 |
+----------+
1 row in set (2.00 sec)

[root@localhost ~]# more /usr/local/mysql/data/slow.log
/usr/libexec/mysqld, version: 5.5.60-mariadb (mariadb server). started with:
tcp port: 0 unix socket: /var/lib/mysql/mysql.sock
time         id command  argument
# time: 180930 23:51:07
# user@host: root[root] @ localhost []
# thread_id: 2 schema:  qc_hit: no
# query_time: 2.001017 lock_time: 0.000000 rows_sent: 1 rows_examined: 0
set timestamp=1538322667;
select sleep(2);

 确认慢查询

mariadb [(none)]> show full processlist; #查看state慢查询在进行
+----+------+-----------+------+---------+------+------------+-----------------------+----------+
| id | user | host   | db  | command | time | state   | info         | progress |
+----+------+-----------+------+---------+------+------------+-----------------------+----------+
| 3 | root | localhost | null | query  |  9 | user sleep | select sleep(10)   |  0.000 |
| 4 | root | localhost | null | query  |  0 | null    | show full processlist |  0.000 |
+----+------+-----------+------+---------+------+------------+-----------------------+----------+
2 rows in set (0.00 sec)

mariadb [(none)]> show full processlist; #查看state慢查询已经结束,但是用户登陆了
+----+------+-----------+------+---------+------+-------+-----------------------+----------+
| id | user | host   | db  | command | time | state | info         | progress |
+----+------+-----------+------+---------+------+-------+-----------------------+----------+
| 3 | root | localhost | null | sleep  |  1 |    | null         |  0.000 |
| 4 | root | localhost | null | query  |  0 | null | show full processlist |  0.000 |
+----+------+-----------+------+---------+------+-------+-----------------------+----------+
2 rows in set (0.00 sec)