How to enable MySQL slow query log
Unusually slow queries are one of the most common MySQL performance issues. Queries that perform acceptably in development may falter under the pressure of a production workload.
Large applications may run hundreds of unique database queries every time an endpoint is hit. This makes it difficult to single out the query that is causing the server to delay response. The MySQL slow query log is a debugging option that can help us identify suspicious SQL statements, providing a starting point for our investigation.
Enable slow query log
The log is a built-in mechanism for recording long-running SQL queries. Queries that do not complete within the configured time are written to the log. Reading the contents of the log will show the SQL executed and the time taken.
By default, slow query logging is turned off. We can activate it on our server by running the following command from the administrative MySQL shell:
SET GLOBAL slow_query_log_file='/var/log/mysql/mysql-slow.log';
SET GLOBAL slow_query_log=1;
The changes take effect immediately. Slow queries will now be logged to /var/log/mysql/mysql-slow.log . We can review this file periodically to identify poorly performing queries.
MySQL counts a query as "slow" if it takes more than 10 seconds to complete. This limit is often too lenient for user-facing web applications that require near-instant responses. We can long_query_time
change the limit by setting the variable:
SET GLOBAL long_query_time=1;
This value sets the minimum duration of slow queries. It is important to find the right balance for our own application. Too high a threshold will exclude queries that actually affect performance. Conversely, a very low value will cause too many queries to be captured, resulting in an overly noisy log.
Using the MySQL configuration file
If you plan to use it long term, you should enable the slow query log in your MySQL configuration file. This will ensure that logging is automatically resumed after a MySQL server restart.
The location of the configuration file may vary depending on the platform distribution. It is usually located at /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf . Add the following line to replicate the settings enabled dynamically above:
slow_query_log=1
slow_query_log_file=/var/log/mysql/mysql-slow.log
long_query_time=1
Restart MySQL for our changes to take effect:
$ sudo service mysql restart
The slow query log is active every time the MySQL server starts.
Customize log content
The logs typically only include SQL queries that exceed the "slow" threshold and are submitted by client applications. This does not include any slow administrative operations that may have occurred, such as index creation and table optimization, and queries that may become slow in the future.
We can extend the log to include this information by making the following changes to the configuration file:
-
log_slow_admin_statements = 1 – includes administrative SQL statements such as
ALTER TABLE
,CREATE INDEX
, ,DROP INDEX
andOPTIMIZE TABLE
. This is rarely desirable, as these operations are typically run during maintenance and migration scripts. Nonetheless, this setting can be useful if our application also performs these tasks dynamically. -
log_slow_replica_statements = 1 – This setting enables slow query logging for replication queries on the replica server. Disabled by default. For MySQL 8.0.26 and earlier, use log_slow_replica_statements instead
log_slow_slave_statements
. - log_queries_not_using_indexes = 1 – When this setting is enabled, queries that are expected to retrieve all records from the target table or view are logged, even if they do not exclude the slow query threshold. This helps determine when a query is missing an index or cannot use it. Queries with available indexes are still logged if there is a lack of constraints limiting the number of rows fetched.
Logging queries that do not use indexes can increase verbosity significantly. In some cases, a full index scan may be needed or required. These queries will continue to show up in the log even if they cannot be resolved.
We can log_throttle_queries_not_using_indexes
limit queries without indexes by setting the _query_info variable. This defines the maximum number of logs that will be written in 60 seconds. A value of 10 means that a maximum of 10 queries will be logged per minute. After the tenth event, no more queries will be logged until the next 60 second window opens.
Explaining the slow query log
Each query that goes into the slow query log will show a set of lines similar to the following:
# Time: 2022-07-12T19:00:00.000000Z
# User@Host: demo[demo] @ mysql [] Id: 51
# Query_time: 3.514223 Lock_time: 0.000010 Rows_sent: 5143 Rows_examined: 322216
SELECT * FROM slow_table LEFT JOIN another_table ...
The commented lines above the query contain the execution time, the MySQL user the client connected to, and statistics providing the duration and number of rows sent. The example above took 3.5 seconds to complete and looked at over 320,000 rows, then only sent 5,143 rows to the client. This may indicate that a missing index is causing MySQL to examine too many records.
We can choose log_slow_extra = 1
to include more information in the log by setting the SYSLOG system variable in the configuration file. This will add the thread ID, the number of bytes received and sent, the number of rows considered sorted, and statement-specific request counts that provide visibility into how MySQL is processing queries.
The log file must be handled with care, as its content is sensitive. Queries are displayed in full, without any masking of parameter values. This means that user data will be present if we use the slow query log on a production server. Access rights should be limited to developers and database administrators who tune SQL statements.
Slow query logging and backup
MySQLDump
A common frustration with the slow query log occurs
when we also use to create database backups. Long-running SELECT * FROM ...
queries will be executed to get data from our tables and feed them to our backups. They will be included in the slow query log like any other SQL statements. If we are doing backups regularly, this can pollute the log.
We can mysqldump
fix this by temporarily disabling the slow query log before running . We can reactivate the log after the backup is complete. Adjust our backup script so that it looks similar to the following:
#!/bin/bash
# Temporarily disable slow query logging
mysql -uUser -pPassword -e "SET GLOBAL slow_query_log=0";
# Run mysqldump
mysqldump -uUser -pPassword --single-transaction databaseName | gzip > backup.bak
# Enable the slow query log again
mysql -uUser -pPassword -e "SET GLOBAL slow_query_log=1"
This will MySQLDump
keep activity away from the slow query log, making it easier to focus on the SQL executed by your application.
Summarize
The MySQL slow query log is one of the most effective ways to pinpoint the cause of performance issues. Start by estimating the latency we are experiencing and use this value as the default long_query_time
. If there is nothing in the log after reproducing the issue, decrease the value.
The slow query log won't tell us the exact fix. However, the ability to see the exact SQL the server received allows us to re-execute poorly performing statements and then measure the effect of the optimization. Adding an index or missing constraint can be the difference between a query involving thousands of rows and one that processes a handful of rows.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Two ways to install mysql-5.5.47 on Linux system and manage mysql
Publish Date:2025/04/26 Views:140 Category:MySQL
-
We know that there are generally two ways to install software on the Linux system. One is to use rpm or yum to install, which is convenient and fast; the other is to use the compiled source package. Although this method is more troublesome
Mysql master-slave replication simple configuration
Publish Date:2025/04/26 Views:120 Category:MySQL
-
I'm learning about MySQL master-slave replication recently and want to apply it to a project at work. Since I'm new to it, I don't understand it very well, so I can only share how to make a simple configuration. At the beginning, we configu
Implementation details of MySQL master-slave replication (I) Exploration of the m
Publish Date:2025/04/26 Views:56 Category:MySQL
-
This article mainly discusses the implementation mechanism of master-slave replication, which may not be directly helpful for our actual application, but understanding its principle can achieve twice the result with half the effort for futu
Implementation details of MySQL master-slave replication (II) Exploration from th
Publish Date:2025/04/26 Views:74 Category:MySQL
-
Previously we explored the master server in master-slave replication. Now let's look at how the slave server works in the entire system. In the article Master Server Exploration, we mentioned that three threads are needed in a master-slave
Mysql master-slave replication - what to do if the slave server stops
Publish Date:2025/04/26 Views:97 Category:MySQL
-
You may find this topic a little ridiculous. What can we do if the server stops? Of course, we should restart the service. Restarting the service is no problem. The problem is that a lot of data has been written to the master database durin
MySQL stored procedure details
Publish Date:2025/04/26 Views:163 Category:MySQL
-
A stored procedure can be thought of as encapsulating a SQL statement that we need to process specially into a function. When needed, we only need to call this function to achieve the desired operation. This process can be called a stored p
How many of these MySQL statement tags have you used?
Publish Date:2025/04/26 Views:122 Category:MySQL
-
In the article "A Peek into MySQL Stored Procedure Details" , we briefly introduced the use of stored procedures. The syntax for creating stored procedures includes BEGIN...END. In addition to BEGIN...END, the following statement tags can b
MySQL CURRENT_TIMESTAMP() Function Detailed Introduction (with Examples)
Publish Date:2025/04/26 Views:132 Category:MySQL
-
This article explains how to use MySQL functions with examples CURRENT_TIMESTAMP() . By using it, we can convert or display the current date and time. The output format is "YYYY-MM-DD HH:MM:SS" or "YYYYMMDDHHMMSS", depending on the context
Back up the MySQL database to a file
Publish Date:2025/04/26 Views:166 Category:MySQL
-
Backing up your database is a very important system administration task that should usually cron be run from a job at scheduled intervals. We will use mysqldump the dump utility that comes with mysql to dump the contents of the database to