Enabling MySQL Slow Query Log & Improve Performance

Updated

mysql slow query log

Three statements, no restart, and they survive a restart because SET PERSIST writes them to mysqld-auto.cnf:

SET PERSIST slow_query_log_file = '/var/log/mysql/mysql-slow.log';
SET PERSIST long_query_time      = 0.2;
SET PERSIST slow_query_log       = ON;

Settings made with SET GLOBAL reset after a MySQL server restart. Adding them to my.cnf to make them permanent is out of date — MySQL 8.0 added SET PERSIST, and on 8.4.10 it works. Measured on a server restarted twice:

before restart:            1 / 0.200000
after restart (SET GLOBAL):  0 / 10.000000        <- back to the defaults
after restart (SET PERSIST): 1 / 0.200000 / /tmp/.../slow.log

SELECT VARIABLE_NAME, VARIABLE_SOURCE, VARIABLE_PATH FROM performance_schema.variables_info ...
+---------------------+-----------------+-------------------------------+
| long_query_time     | PERSISTED       | .../data/mysqld-auto.cnf      |
| slow_query_log      | PERSISTED       | .../data/mysqld-auto.cnf      |
| slow_query_log_file | PERSISTED       | .../data/mysqld-auto.cnf      |
+---------------------+-----------------+-------------------------------+

performance_schema.variables_info is how you prove where a setting came from — COMPILED, GLOBAL, PERSISTED or a config file path. Both restarts, the defaults, the dynamic-variable list and the log entries below are repeated by how-to-enable-slow-query-log.sh on a throwaway mysqld at port 33907; a server you already run is never contacted.

The log path fails silently in the docs and loudly in practice

If you set slow_query_log_file = '/var/log/mysql/mysql-slow.log' and that directory does not exist, or mysqld cannot write to it, you do not get a warning:

SET GLOBAL slow_query_log_file = '/var/log/mysql/mysql-slow.log';
ERROR 1231 (42000): Variable 'slow_query_log_file' can't be set to the value of
'/var/log/mysql/mysql-slow.log'

Fix the directory first (mkdir -p /var/log/mysql && chown mysql:mysql /var/log/mysql), or keep the default, which lives in the data directory and is already writable. SET GLOBAL slow_query_log = 'ON' with the quoted string does work; so does = 1 and = ON.

Every one of these is dynamic on 8.4.10 — nothing needs a restart

Setting each in turn and checking for an error:

DYNAMIC   slow_query_log
DYNAMIC   long_query_time
DYNAMIC   log_output
DYNAMIC   log_queries_not_using_indexes
DYNAMIC   min_examined_row_limit
DYNAMIC   log_slow_extra
DYNAMIC   log_slow_admin_statements
DYNAMIC   log_throttle_queries_not_using_indexes

Defaults out of the box: slow_query_log=OFF, long_query_time=10.000000, log_output=FILE, log_queries_not_using_indexes=OFF, min_examined_row_limit=0, log_slow_extra=OFF. long_query_time takes fractions — 0.2 is legal and far more useful than the default 10 seconds, which on most web workloads logs nothing at all.

If you lower it to 0.2 and the log stays empty while the application is still slow, the queue is somewhere else. Three one-second queries take three seconds down a single connection and one second across three, measured on this same 8.4.10 server: a connection is a single server thread, and it runs your statements one after another whatever the client library calls itself.

A real slow query and the exact log entry it produced

2,000,000 orders, no index on status or customer_id, with long_query_time = 0.2 and log_slow_extra = ON:

SELECT customer_id, count(*) AS orders, sum(total) AS lifetime
FROM orders WHERE status = 'paid' GROUP BY customer_id ORDER BY lifetime DESC LIMIT 5;
# Time: 2026-08-10T22:53:29.958435Z
# User@Host: root[root] @ localhost []  Id:    29
# Query_time: 0.483066  Lock_time: 0.000002 Rows_sent: 5  Rows_examined: 2000005
Thread_id: 29 Errno: 0 Killed: 0 Bytes_received: 155 Bytes_sent: 251 Read_first: 1
Read_last: 0 Read_key: 666667 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 2050002
Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 5 Sort_scan_count: 1
Created_tmp_disk_tables: 0 Created_tmp_tables: 1
Start: 2026-08-10T22:53:29.475369Z End: 2026-08-10T22:53:29.958435Z
SET timestamp=1786402409;
SELECT customer_id, count(*) AS orders, sum(total) AS lifetime
FROM orders WHERE status = 'paid' GROUP BY customer_id ORDER BY lifetime DESC LIMIT 5;

Rows_sent: 5 against Rows_examined: 2000005 is the whole diagnosis: the server read every row to return five. Everything from Thread_id onwards exists only because log_slow_extra is on; without it the header stops after Rows_examined. Created_tmp_disk_tables: 1 on the data-loading statement in the same log is how you spot a query that spilled a temporary table to disk.

log_queries_not_using_indexes logs fast queries too

It is not a slowness filter. With long_query_time back at 10 seconds and nothing remotely slow happening, five queries were run — one primary-key lookup, three 2,000,000-row scans and one scan of a five-row table — and four of the five were logged. Only the primary-key lookup escaped:

# Query_time: 0.158153 ... Rows_examined: 2000000
SELECT count(*) FROM orders WHERE customer_id = 1;

On a busy server this fills a disk. Pair it with log_throttle_queries_not_using_indexes (a cap per minute per query, default 0 = no limit) and with min_examined_row_limit, which suppresses entries below a row count. Setting min_examined_row_limit = 100000 kept the three big scans and dropped the five-row one — which is the right trade: a table scan is only worth logging when the table is big enough for it to matter.

log_output = TABLE costs you the extra columns

SET GLOBAL log_output = 'TABLE' routes entries to mysql.slow_log, which is queryable with SQL — convenient, and the timings keep microsecond precision:

   query_time: 00:00:00.401361
rows_examined: 2000003
     sql_text: SELECT customer_id, count(*) c FROM orders WHERE status='pai

CREATE TABLE `slow_log` (
  `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ...,
  `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL,
  `rows_sent` int NOT NULL, `rows_examined` int NOT NULL, `db` varchar(512) NOT NULL,
  `last_insert_id` int NOT NULL, `insert_id` int NOT NULL, `server_id` int unsigned NOT NULL,
  `sql_text` mediumblob NOT NULL, `thread_id` bigint unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8mb3 COMMENT='Slow log'

That is a fixed 12-column CSV table: there is nowhere for the log_slow_extra fields to go, so Rows_examined survives but Created_tmp_disk_tables, Sort_merge_passes and the rest are lost. mysqldumpslow also only reads the file. log_output = 'FILE,TABLE' is accepted and gives you both.

Aggregate before you optimise

$ mysqldumpslow -s t /path/to/slow.log
Count: 1  Time=0.43s (0s)  Lock=0.00s (0s)  Rows=3.0 (3), root[root]@localhost
  SELECT customer_id, count(*) c FROM orders WHERE status='S' GROUP BY customer_id ORDER BY c DESC LIMIT N

mysqldumpslow ships with MySQL and normalises literals to S and N so repeats collapse into one line. Sort by -s t (total time) rather than by the single worst query: the statement that runs 10,000 times at 0.3 s is the one holding your server down.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.