This article describes the MySQL slow query operation with examples. Share with you for your reference, the details are as follows: Some MySQL SQL statements may execute very slowly, which may cause a surge in server load. First, check to make sure that MySQL is the one that is affecting the load, using the top command, ps command, etc. Next, enter MySQL and use the show full processlist command to query the SQL statements being executed to see the problem. Use the explain command to view the status. Finally find out the SQL statement to kill or optimize Install mariadb service on centos7 yum -y install mariadb-server mariadb-devel Enable slow query 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 Start mariadb service systemctl start mariadb Check whether MySQL slow query is enabled and how long the above is a slow query 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) #If you don't have slow query enabled, you can enable it in the command line mysql> set global slow_query_log=1; Query OK, 0 rows affected (0.00 sec) Test slow queries and view logs 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); Confirming slow queries MariaDB [(none)]> show full processlist; #View the state slow query in progress +----+------+-----------+------+---------+------+------------+-----------------------+----------+ | 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; #Check the state slow query has ended, but the user is logged in +----+------+-----------+------+---------+------+-------+-----------------------+----------+ | 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) Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: Steps to configure IIS10 under Win10 and support debugging ASP programs
>>: Example of how to achieve ceiling effect using WeChat applet
In JavaScript's DOM event model, events are r...
What is NFS? network file system A method or mech...
1. Linux kernel driver module mechanism Static lo...
Preface : Today I was asked, "Have you carefu...
Recently, I have been working on a large-screen d...
First check the kernel version you are using lin@...
1. Install shadowsocks sudo apt-get install pytho...
Table of contents Start and stop Database related...
Table of contents Initialize computed Dependency ...
MySQL 5.0 has become a classic because of its few...
This article shares with you the MySQL 8.0.17 ins...
About CSS3 variables When declaring a variable, a...
This is a collection of commonly used but easily ...
Here I use samba (file sharing service) v4.9.1 + ...
Docker is becoming more and more popular. It can ...