The following articles mainly introduce the MySQL slow query analysis method. a few days ago, I set a record to query SQL statements that are slower than 1 second in the MySQL database. There are several very well-configured methods, and the names of several parameters cannot be remembered, so the MySQL Query is repeated.
The following articles mainly introduce the MySQL slow query analysis method. a few days ago, I set a record to query SQL statements that are slower than 1 second in the MySQL database. There are several very well-configured methods, and the names of several parameters cannot be remembered, so I made a note again.
For troubleshooting and identifying performance bottlenecks, the most common problems are slow MySQL queries and queries without indexes.
OK. start to find the SQL statement that is not "refreshing" in MySQL.
MySQL slow query Analysis Method 1:
I am using this method.
MySQL and later versions support recording slow SQL statements.
- MySQL> show variables like 'long%';
Note: This long_query_time is used to define how many seconds are slower to calculate as "slow query"
- +-----------------+-----------+
- | Variable_name | Value |
- +-----------------+-----------+
- | long_query_time | 10.000000 |
- +-----------------+-----------+
- 1 row in set (0.00 sec)
- MySQL> set long_query_time=1;
Note: I set 1, that is, if the execution time exceeds 1 second, the query is slow.
- Query OK, 0 rows affected (0.00 sec)
- MySQL> show variables like 'slow%';
- +---------------------+---------------+
- | Variable_name | Value |
- +---------------------+---------------+
- | slow_launch_time | 2 |
- | slow_query_log | ON |
Note: whether to enable logging
- | slow_query_log_file | /tmp/slow.log |
Note: where to set
- +---------------------+---------------+
- 3 rows in set (0.00 sec)
- MySQL> set global slow_query_log='ON'
Note: Enable logging
Once the slow_query_log variable is set to ON, MySQL starts recording immediately.
In/etc/my. cnf, you can set the initial values of the above MySQL global variables.
- long_query_time=1
- slow_query_log_file=/tmp/slow.log
MySQL slow query Analysis Method 2:
MySQLdumpslow command
- /path/MySQLdumpslow -s c -t 10 /tmp/slow-log
This will output 10 SQL statements with the maximum number of records, of which:
-S indicates the sorting method. c, t, l, and r are sorted by the number of records, time, query time, and number of returned Records, ac, at, al, and ar indicate reverse descriptions;
-T indicates the top n, that is, the number of previous data records returned;
-G, followed by a regular expression matching mode, which is case insensitive;
For example
- /path/MySQLdumpslow -s r -t 10 /tmp/slow-log
You can obtain up to 10 queries from the returned record set.
- /path/MySQLdumpslow -s t -t 10 -g “left join” /tmp/slow-log
Obtain the query statements containing the left join in the first 10 results sorted by time.
The above content is an introduction to MySQL slow query analysis. I hope you will get some benefits.