Quick revision
The short answer
In MySQL we enable slow_query_log with a long_query_time such as 0.5 seconds, optionally log_queries_not_using_indexes, and summarise the log with pt-query-digest, or read the statement digest tables in performance_schema. In PostgreSQL, log_min_duration_statement logs statements slower than a threshold, pg_stat_statements, loaded through shared_preload_libraries, aggregates normalised queries with calls, total_exec_time, mean_exec_time, rows and buffer counts, and auto_explain logs the plans of slow statements. Whichever tool we use, we sort by total time to find what costs the most overall, not only by mean time.
What to remember
In this answer, we found slow queries in MySQL with the slow query log, long_query_time and optionally log_queries_not_using_indexes, summarised with pt-query-digest or read from performance_schema digest tables. In PostgreSQL we used log_min_duration_statement for slow statements, pg_stat_statements loaded through shared_preload_libraries for calls, total and mean execution time, rows and buffers per normalised query, and auto_explain for their plans. Finally, we saw why sorting by total time finds what costs the most overall.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In MySQL we enable slow_query_log with a long_query_time such as 0.5 seconds, optionally log_queries_not_using_indexes, and summarise the log with pt-query-digest, or read the statement digest tables in performance_schema. In PostgreSQL, log_min_duration_statement logs statements slower than a threshold, pg_stat_statements, loaded through shared_preload_libraries, aggregates normalised queries with calls, total_exec_time, mean_exec_time, rows and buffer counts, and auto_explain logs the plans of slow statements. Whichever tool we use, we sort by total time to find what costs the most overall, not only by mean time.
Why this question is asked
Before we can optimise anything, we need to know which queries deserve attention, and guessing is usually wrong: the query everyone complains about is often not the one using most of the database. Interviewers ask this to check that we know the standard tools of both major open-source databases and, more importantly, how to read what they tell us. In this answer, we will set up MySQL's slow query log and its summaries, look at performance_schema digests, then set up PostgreSQL's duration logging, pg_stat_statements and auto_explain, and finish with why sorting by total time matters.
1MySQL: The Slow Query Log
MySQL can write every statement that runs longer than a threshold to a log file, the slow query log. It is off by default. We enable it at runtime with SET GLOBAL or in the configuration file.
Each entry records the query text, its time, lock time, rows sent and rows examined. Rows examined compared with rows sent is a great quick signal: a query that examines 2 million rows to return 10 is probably missing an index. log_queries_not_using_indexes also logs fast queries that scan without an index, which finds problems before tables grow, but on a system with many small tables it can be noisy, so it is often enabled only for a while.
A raw log with thousands of entries is hard to read, so we summarise it. pt-query-digest from Percona Toolkit groups entries by normalised query, replacing literal values with placeholders, and prints a ranked report with each query's total time, count, average and percentiles.
2MySQL: performance_schema Digests
performance_schema, MySQL's built-in instrumentation, keeps aggregated statistics per normalised statement in memory, without any log file. The sys schema offers friendlier views on top of it.
This captures every statement, not only those above long_query_time, so it also shows the fast but frequent queries that the slow log never sees. The counters are cumulative since the server started or since they were last truncated.
3PostgreSQL: Logging Slow Statements
PostgreSQL's equivalent of the slow log is a setting rather than a separate file. log_min_duration_statement logs every statement that runs longer than the given time to the server log, with its duration.
Notice that with prepared statements the log line shows placeholders and the parameters may be logged separately, which matters because logged parameters can contain personal data. It can also be set per database or per role, for example lower for a reporting role.
4PostgreSQL: pg_stat_statements and auto_explain
pg_stat_statements is the most useful tool in PostgreSQL for this. It is an extension shipped with PostgreSQL that tracks statistics for every normalised statement: how many times it ran, total and mean execution time, rows, and how many blocks it hit in cache or read. It needs shared memory, so it must be loaded at server start.
| Need | MySQL | PostgreSQL |
|---|---|---|
| Log statements slower than X | slow_query_log + long_query_time | log_min_duration_statement |
| Aggregate by normalised query | performance_schema digests, sys views | pg_stat_statements |
| Summarise a log | pt-query-digest | pgBadger or similar log analysers |
| Plans of slow statements | EXPLAIN ANALYZE by hand | auto_explain |
| Queries scanning without index | log_queries_not_using_indexes | seq_scan counts in pg_stat_user_tables |
The column names total_exec_time and mean_exec_time apply from PostgreSQL 13; older versions called them total_time and mean_time. pg_stat_statements tells us which statements cost the most, but not why. For that we need their plans, and auto_explain, loaded in the same line above, logs the execution plan of any statement slower than auto_explain.log_min_duration, optionally with actual rows and buffers. Together they answer what is expensive and how it runs.
5Sort by Total Time, Not Only by Mean
Once the data is there, the most important decision is how to sort it. Sorting by mean time shows the individually slowest queries, the ones users notice on a single page. Sorting by total time, mean times calls, shows where the database actually spends its time. These lists are often very different.
Consider a report that takes 8 seconds and runs 50 times a day: 400 seconds of database time. A lookup that takes 2 ms and runs 30 million times a day costs 60,000 seconds, 150 times more. Making the report twice as fast saves 200 seconds; halving the lookup's cost, or its number of calls, saves 30,000. So we look at both lists, but capacity problems are almost always found at the top of the total time list.
6Follow-up questions
Why is a query with 2 ms mean time sometimes the biggest problem?
Because cost is mean time multiplied by calls. A 2 ms query called 30 million times a day uses 60,000 seconds of database time, far more than a slow report that runs a few times. It often comes from an N+1 pattern or polling in the application, so the best fix is frequently to call it less, with batching or caching, rather than to make each call faster.
Does enabling these tools slow the database down?
pg_stat_statements and performance_schema have a small overhead that is accepted almost everywhere in production. Logging every statement or setting very low thresholds can produce huge logs and measurable I/O, and auto_explain with log_analyze adds instrumentation to every statement, so we use sensible thresholds, sampling and timing off where available.
How do I measure the effect of a fix?
Take a snapshot of the statistics before and after, or reset them with pg_stat_statements_reset() in PostgreSQL or by truncating the digest table in MySQL, and compare total time, calls and blocks per call over the same period of similar traffic.
7Conclusion
In this answer, we found slow queries in MySQL with the slow query log, long_query_time and optionally log_queries_not_using_indexes, summarised with pt-query-digest or read from performance_schema digest tables. In PostgreSQL we used log_min_duration_statement for slow statements, pg_stat_statements loaded through shared_preload_libraries for calls, total and mean execution time, rows and buffers per normalised query, and auto_explain for their plans. Finally, we saw why sorting by total time finds what costs the most overall.
Open with
In MySQL I would turn on the slow query log with slow_query_log, set long_query_time to something like half a second, and maybe enable log_queries_not_using_indexes for a while to catch scans early.
Then, if they want more
Each entry shows time, rows examined and rows sent, and a big gap between examined and sent usually means a missing index. To make sense of the log I would run pt-query-digest, which groups statements by their normalised form and ranks them.
MySQL also has performance_schema, where events_statements_summary_by_digest aggregates every statement, including fast ones the slow log never sees, and the sys schema has friendlier views on top. In PostgreSQL, log_min_duration_statement logs statements slower than a threshold.
The main tool is pg_stat_statements: it has to be in shared_preload_libraries and created as an extension, and then it gives me calls, total_exec_time, mean_exec_time, rows and shared blocks hit and read for every normalised query. To see why a query is slow, auto_explain logs the plans of statements above a duration. The most important thing is how I sort.
Sorting by mean time shows the slowest single queries, but sorting by total time shows where the database actually spends its time, and a 2 millisecond query called millions of times often costs far more than an 8 second report.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 13
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you find the slowest queries in MySQL and in PostgreSQL? Name the settings or...": https://scrivio-interview-library.netlify.app/databases/query-optimization/2976fed12d/