Sub topic 6
Slow per call versus expensive in aggregate
This chapter assumes that you know basic SQL, can query pg_stat_statements, and have a general idea of what a query plan is. The goal is to choose the right ranking for the performance question you are asking.
A query can be slow each time it runs, or it can consume a large amount of database time because it runs very often. These are different problems. pg_stat_statements helps separate them with mean_exec_time, total_exec_time, and calls.
Two useful questions
Use total execution time to ask:
- Which query consumed the most database time during this measurement period?
- Which query is the largest overall workload cost?
Use mean execution time to ask:
- Which query is slow on a typical execution?
- Which query might make one request wait for a long time?
The calls column gives context. A query with a high mean time and five calls may need attention, but a query with a modest mean time and millions of calls may consume more capacity.
| Query pattern | Mean time | Calls | Total impact |
|---|---|---|---|
| Slow report | 2,000 ms | 10 | 20,000 ms |
| Frequent lookup | 5 ms | 100,000 | 500,000 ms |
The frequent lookup is the larger aggregate cost even though each execution is fast.
Rank by total cost
This query finds aggregate heavy hitters:
SELECT queryid,
calls,
round(total_exec_time::numeric, 2) AS total_ms,
round(mean_exec_time::numeric, 2) AS mean_ms,
rows,
left(query, 80) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;total_exec_time is cumulative execution time for the normalized statement. It does not include time waiting for a client to read results. The values are measured in milliseconds.
Start with this ranking when investigating overall database capacity. A statement near the top may be inexpensive per call but important because application code executes it repeatedly.
Rank slow calls while filtering noise
A one-time migration or unusual report can have a high mean time without being a recurring problem. Add a minimum call count when looking for statements that are repeatedly slow:
SELECT queryid,
calls,
round(mean_exec_time::numeric, 2) AS mean_ms,
round(max_exec_time::numeric, 2) AS max_ms,
left(query, 80) AS query
FROM pg_stat_statements
WHERE calls >= 50
ORDER BY mean_exec_time DESC
LIMIT 10;The call threshold is a measurement choice. A threshold of 50 is useful for a busy service, but it may hide important queries on a low-volume system. Choose a value that represents repeated behavior in your workload.
mean_exec_time is an average. It can hide variation. For example, 99 executions at 5 milliseconds and one execution at 5 seconds produce a mean of about 55 milliseconds. Use min_exec_time, max_exec_time, and standard deviation in a separate analysis when consistency matters.
A practical decision path
After selecting a statement, use its queryid and actual parameter values to investigate it with EXPLAIN (ANALYZE, BUFFERS). The statistics ranking identifies candidates; the execution plan helps explain why they are slow.
Exercise
A query has a mean execution time of 4 ms and 200,000 calls. Another has a mean of 900 ms and 20 calls. Which query has the greater cumulative execution time?
Show answer
The first query: 4 × 200,000 = 800,000 ms. The second uses 900 × 20 = 18,000 ms. Rank by mean time as well if the second query harms individual request latency.