Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Slow Query Log

Direct answer

The slow query log records queries slower than a threshold (MySQL slow_query_log/long_query_time; PostgreSQL log_min_duration_statement; SQL Server via Query Store/Extended Events).

When to use this

Use it to find which queries to optimise.

First 30 seconds

1. Enabled and threshold set?

2. Aggregate: pt-query-digest slow.log / pgBadger.

3. Top queries by total time.

4. EXPLAIN them.

5. Re-measure.

Operational verification

query exceeds threshold → logged → aggregated → optimised

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it to find which queries to optimise.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Compare the top queries before and after a fix. Keep the threshold low enough to capture problems without filling the disk.

Step 2 Common failures Narrow the fault

A threshold of 0 on a busy server floods the log. Looking only at the slowest single query misses frequent medium-slow queries. Logs on the same disk as data add I/O.

Key points

Aggregate the log (pt-query-digest, pgBadger) rather than reading raw lines; optimise by total time. Avoid too-low thresholds on busy servers.

Key checks
  • pt-query-digest slow.log — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand

How it works

The server writes each slow statement with its duration, rows examined and lock time. Aggregation groups similar statements by fingerprint, showing count, total and average time.

ReferenceImportant terms & boundaries2 sections · depth on demand

In depth

Reading one slow-log entry

# Time: 2026-11-27T09:14:03.512345Z
# User@Host: shop_app[shop_app] @ [10.0.4.17]
# Query_time: 3.120000  Lock_time: 0.000150 Rows_sent: 10  Rows_examined: 500000
SET timestamp=1795770843;
SELECT * FROM orders WHERE note LIKE '%refund%' ORDER BY created_at DESC LIMIT 10;

Rows_examined 50,000 times larger than Rows_sent says the query scans instead of seeking — an index or query rewrite problem, not a hardware problem. High Lock_time points at contention instead.

Find the queries that cost the most in total

One 10-second report a day matters less than a 50 ms query that runs 2 million times. Aggregate by fingerprint:

mysqldumpslow -s t -t 10 /var/log/mysql/slow.log     # top 10 by total time
pt-query-digest /var/log/mysql/slow.log | less          # Percona Toolkit, more detail

PostgreSQL equivalents

log_min_duration_statement = 500       # ms; log statements slower than this
auto_explain.log_min_duration = 2000   # log the plan of very slow statements (load auto_explain)
-- pg_stat_statements: totals per normalised query, no log parsing needed
SELECT calls, round(total_exec_time) AS total_ms, round(mean_exec_time,1) AS mean_ms, left(query,80)
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;

Practical thresholds

Start at 1 s on a busy system and lower it (to 100–500 ms) once the worst offenders are fixed. A threshold of 0 logs everything and can itself become the bottleneck and fill the disk; for full capture use sampling or performance_schema / pg_stat_statements instead.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Slow log threshold too high to catch problems.
ReferenceConfiguration & state1 section · depth on demand

Enabling slow query logs

# MySQL / MariaDB
slow_query_log = 1
long_query_time = 1
log_queries_not_using_indexes = 0
# PostgreSQL
log_min_duration_statement = 1000      # ms

Analyse with pt-query-digest (MySQL), pg_stat_statements (PostgreSQL: SELECT query, calls, mean_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;) or Query Store in SQL Server. Focus on total time (calls × mean), not only the slowest single query.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
slow query logslow query logpgbadger pt-query-digest
Confirmation

Confirm action

Continue?