A slow database rarely announces itself. Pages load a little later, the admin area drags, and CPU usage creeps up. MySQL slow query logging gives you a list of the exact queries behind that slowdown, with timings attached. The catch is that logging itself costs resources, and on a small VPS that cost matters. This guide shows how to set it up so the log helps you instead of adding to the problem.
How MySQL Slow Query Logging Works
When MySQL slow query logging is on, the server writes any query that takes longer than a threshold you choose into a log file. Each entry records how long the query ran, how many rows it examined, and how many rows it sent back. That combination tells you more than timing alone: a query that examines 200,000 rows to return 10 has a missing index, while one that examines 12 and returns 12 is slow for a different reason.
The log is a diagnostic tool, not a permanent fixture. You turn it on, collect data for a day or two, fix what it finds, and then either turn it off or leave it running with a tight threshold. Running it forever with loose settings on a 1 GB or 2 GB VPS is how a helpful tool turns into a disk and I/O problem.
Will the Slow Query Log Slow Down Your VPS?
It adds some overhead, but far less than most people fear when it is configured with care. MySQL only writes to the log when a query crosses your threshold, so with a sensible threshold the log stays small and the write volume stays low. The overhead grows when the threshold is too low, when logging of unindexed queries is on, or when the log file lives on a slow disk with no rotation.
Three settings decide almost all of the cost: long_query_time, the threshold in seconds; log_queries_not_using_indexes, which also logs fast queries without an index; and min_examined_row_limit, which skips queries that examined very few rows. Get those three right and the log costs you almost nothing in practice. If you want a broader picture of what a healthy MySQL setup looks like on limited RAM, the MySQL 8.4 tuning settings that work on a 2 GB VPS cover the memory side of the same problem.
Enable the Slow Query Log in MySQL 8
Start with the runtime method, which takes effect immediately and disappears on restart, so it is safe for a first test:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL slow_query_log_file = '/var/log/mysql/mysql-slow.log';
SET GLOBAL long_query_time = 2;
SET GLOBAL log_queries_not_using_indexes = 'OFF';
SET GLOBAL min_examined_row_limit = 100;
Check that it took effect with SHOW VARIABLES LIKE 'slow_query%';. Then run a query you know is slow, or just wait for normal traffic, and confirm entries appear in the log file. If it stays empty after real traffic, the threshold is probably too high, or the log path is not writable by the mysql user. Fix the directory permissions before changing anything else.
Once you are happy with the settings, make them permanent in your MySQL config file, usually /etc/mysql/my.cnf or a file under /etc/mysql/mysql.conf.d/:
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
log_queries_not_using_indexes = 0
min_examined_row_limit = 100
Restart MySQL after editing the file, and double-check that the service comes back cleanly. That is where monitoring MySQL uptime pays for itself: if the restart fails at 3 a.m., you want to hear about it before your visitors do.
Picking the Right long_query_time
There is no single correct value. It depends on what your site normally does. A threshold of 2 seconds is a solid starting point for most WordPress sites and small apps. It catches the queries that actually hurt users and ignores the background noise.
If your log fills up within an hour, raise the threshold to 5 seconds and look at the worst offenders first. If it stays empty for a full day while pages feel slow, lower it to 1. The goal is a log you can read in ten minutes, not a file you have to grep through for an hour.
Leave log_queries_not_using_indexes off unless you have a specific reason. On a WordPress site it logs a flood of tiny queries that ran in milliseconds. The signal drowns in the noise, and on a small VPS the write volume is the real cost. Turn it on for a short, targeted session when you suspect missing indexes, then turn it back off. And if you want to tune memory alongside logging, check the InnoDB buffer pool sizing guide first, since a badly sized buffer pool creates slow queries that no amount of logging will fix on its own.
Reading the Log with mysqldumpslow and pt-query-digest
A raw slow log is hard to read: the same query appears dozens of times with different values. Use a summarizer instead.
mysqldumpslow ships with MySQL and needs no install. It groups similar queries and sorts them by total time:
mysqldumpslow -s t -t 10 /var/log/mysql/mysql-slow.log
That shows the 10 queries that consumed the most total time. The -s t flag sorts by time; -s c sorts by count, which is useful when a fast query runs thousands of times and adds up.
For deeper analysis, pt-query-digest from the Percona Toolkit gives a full report with per-query fingerprints and time distribution. Install it from your distribution’s repository and run it against the log file.
Whatever tool you use, work from the summary, not the raw file, and fix the top three queries first. In most cases that means adding an index, rewriting a query that scans a whole table, or removing a plugin that runs an expensive query on every page load. The companion guide on finding and fixing the slow queries it surfaces walks through that part step by step.
Keep the Log Small on a Small VPS
A slow log with no rotation grows until the disk fills. On a VPS with 20 or 40 GB of disk that happens faster than you expect, and a full disk stops MySQL entirely. Set up rotation before you need it.
MySQL ships a logrotate config on most distributions, but check that it actually covers the slow log path. A minimal /etc/logrotate.d/mysql-slow looks like this:
/var/log/mysql/mysql-slow.log {
daily
rotate 7
compress
missingok
create 640 mysql adm
sharedscripts
postrotate
mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logs
endscript
}
Seven days of compressed logs is plenty for diagnosis. If disk space is already tight, drop rotate to 3 and read the logs sooner. Our guide on how to keep MySQL disk usage under control covers the other places MySQL quietly eats disk, like binary logs and oversized tables.
Two more habits help. Put the log on the same disk as your data directory, not a network mount, so writes never wait on the network. And turn logging off when you are done diagnosing: a log you never read is pure overhead.
Common Mistakes to Avoid
The most common mistake is setting long_query_time to 0 or 0.1 and logging everything. That turns the log into a copy of all traffic, and on a busy site the write load is noticeable. Start at 2 seconds and lower it only if the log is empty.
The second is enabling logging and never reading the file. The log fixes nothing by itself. Block 15 minutes a day after enabling it, read the summary, and act on the top entries.
The third is forgetting the log exists after a migration or a MySQL upgrade. Config files get replaced, paths change, and the log silently stops. After any upgrade, run SHOW VARIABLES LIKE 'slow_query%'; and confirm it is still on with the settings you expect.
Finally, do not chase every entry. A query that runs once a day and takes 3 seconds is not the problem; one that runs 500 times an hour at 0.5 seconds each is. Total time matters more than single-query time, which is why sorting by total time is the first thing to do.
What to Do After You Find the Slow Queries
Add the missing index first, since that fixes the largest group of slow queries with the least effort. Then look for queries that scan whole tables on every request, often from a plugin or a custom report page. Cache the result or rewrite the query so it only reads the rows it needs.
Re-run the log after each fix and compare the top 10 before and after. If the same query still sits at the top, the fix did not take, or the query needs a different index than the one you added. Keep the log running for a few days after the last fix, then turn it off until the next time pages feel slow. That cycle, log, fix, verify, is the whole method. It costs almost nothing on a small VPS, and it replaces guessing with a list of named queries and exact timings.