Slow query log

On this page 7

A slow query log is a database log that records SQL statements whose execution time exceeds a configured threshold, together with details such as rows examined, lock time and the client that sent them. In database performance work, it is the primary evidence for deciding which queries to optimize first.

The term comes from MySQL and MariaDB, where the feature carries this name, but the same idea exists in PostgreSQL as duration logging and in other systems as query statistics views. Whatever the implementation, the log turns vague complaints about a slow database into a concrete list of statements with measurable cost.

How the slow query log captures statements

After each statement finishes, the server compares its execution time with the configured threshold. If the statement took longer, the server writes an entry to a file or a table containing the SQL text and execution statistics. Because the check happens at completion, a query that is still running does not appear until it ends.

Each entry contains several fields that together describe where the time went. The most useful ones are listed below in the form used by MySQL and MariaDB.

FieldMeaningWhat it reveals
Query_timeTotal execution time in secondsOverall cost of the statement
Lock_timeTime spent waiting for locksContention rather than slow execution
Rows_sentRows returned to the clientSize of the result
Rows_examinedRows read to produce the resultMissing or unused indexes
Timestamp and user@hostWhen and from where it ranWhich application or job is responsible
Fields in a typical slow query log entry

The ratio of Rows_examined to Rows_sent is often the fastest diagnostic. A query that reads a million rows to return ten is almost always missing a suitable index or filtering on an expression the index cannot use.

Configuration parameters and sensible values

The log is controlled by a small set of variables that can usually be changed at runtime without a restart. The right threshold depends on the workload: an interactive web application cares about tens of milliseconds, while a reporting system may tolerate several seconds.

SettingSystemPurposeCommon value
slow_query_logMySQL, MariaDBTurns the log on or offON in production
long_query_timeMySQL, MariaDBThreshold in seconds, fractions allowed0.1-2
log_queries_not_using_indexesMySQL, MariaDBLogs full scans regardless of timeON briefly during audits
min_examined_row_limitMySQL, MariaDBSkips queries reading few rows100-1,000
log_min_duration_statementPostgreSQLThreshold in milliseconds200-1,000
auto_explain.log_min_durationPostgreSQLAlso logs the execution planEqual to or above main threshold
Main slow query log settings

Setting the threshold to zero logs every statement, which is useful for short sampling sessions but produces large files and measurable overhead on busy servers. Log rotation must be configured before any low threshold is left in place.

Analyzing the log effectively

Raw entries are rarely read one by one. Analysis tools normalize statements into fingerprints by replacing literal values with placeholders, so thousands of executions of the same query collapse into one line with total and average time.

Ranking by total time consumed usually matters more than ranking by the slowest single execution. A query taking 50 milliseconds but running a million times per day costs far more than a nightly report that takes two minutes once. Tools such as mysqldumpslow, pt-query-digest and pgBadger produce this kind of ranking.

ToolData sourceStrengthLimitation
Slow query logCompleted statements over a thresholdFull SQL text with real valuesMisses fast but frequent queries
Performance Schema digestsIn-memory statement statisticsAggregates every query cheaplyResets on restart
pg_stat_statementsPostgreSQL extensionTotals per normalized queryNo individual examples
EXPLAIN ANALYZEOne query run on demandExact execution planRequires knowing the query first
Slow query log and related diagnostic tools

From log entry to fix

The log points at a problem; query optimization solves it. Typical patterns in the log map to a small set of remedies, summarized below.

Pattern in the logLikely causeTypical fix
High Rows_examined, few rows sentMissing indexAdd a selective or composite index
High Lock_timeBlocking transactionsShorten transactions, review locking
Same query thousands of timesLoop issuing one query per rowBatch into a single set-based query
Large OFFSET valuesDeep paginationSwitch to keyset pagination
Spikes at fixed timesScheduled jobsMove jobs off-peak or to a replica
Patterns found in the log and their usual fixes

Limitations and pitfalls

  • The log can contain sensitive literal values such as email addresses, so access to it must be restricted.
  • A slow query may be a victim rather than a cause, delayed by locks or by an overloaded disk.
  • Logging to a table instead of a file adds overhead and can itself become contended.
  • Queries slightly below the threshold stay invisible even when they dominate total load.
  • Thresholds are global by default, so one setting rarely suits both interactive and batch workloads.

A plain-language summary

A slow query log works like a notebook where the database writes down every request that took too long. The administrator decides what too long means, for example half a second.

Later, someone reads the notebook to see which requests appear most often and which waste the most time. Instead of guessing why a website feels sluggish, the team gets a list of exact culprits. Fixing the top few entries usually brings the biggest improvement.

An example from practice

An e-commerce site becomes slow every weekday morning. The database administrator enables the slow query log on the MariaDB server with long_query_time set to 0.2 seconds and collects one day of entries, then runs a digest tool over the file.

The report shows that one product search query accounts for about 40% of all logged time. Each execution examines around 200,000 rows to return 20 results, because the filter combines category and price while only the category is indexed. Adding a composite index on category and price reduces rows examined to a few hundred. The query drops out of the slow log entirely, and morning page load times return to normal.