Slow query - MySQL

Page content

What is slow query?

https://dev.mysql.com/doc/refman/8.0/en/slow-query-log.html

The definition “slow query” is “SQL statements that take more than long_query_time seconds to execute and require at least min_examined_row_limit rows to be examined”.

Configure

Check current status

SHOW VARIABLES LIKE "slow_query_log";
SHOW VARIABLES LIKE "slow_query_time";
SHOW VARIABLES LIKE "slow_query_log_file";

Enable to log slow queries

Logging slow queries which take more than 10 seconds.

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 10;
SET GLOBAL slow_query_log_file = '/path/filename';

A sample line in the log file:

# User@Host: atlex00[atlex00] @  [192.168.0.123]  Id: 123456789
# Query_time: 12.34556  Lock_time: 0.000083 Rows_sent: 0  Rows_examined: 1234567
SELECT .... {{ long_query_content }}