A slow query lands in your lap. You run EXPLAIN ANALYZE and get back a wall of indented text that looks like someone fed a query plan through a blender. Most developers stare at it for a few minutes and give up.
You do not need to understand every node. There are five patterns that cover the vast majority of real-world slow queries, and once you can spot them, the output goes from intimidating to immediately actionable.
Run it
EXPLAIN ANALYZE
SELECT u.name, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.id, u.name
ORDER BY order_count DESC
LIMIT 10;
Add BUFFERS to also see cache hit rates:
EXPLAIN (ANALYZE, BUFFERS)
SELECT ...
Read the output
The result is a tree of nodes, indented to show parent-child relationships. Each node performs one step of the query. The overall query is the root node; the deepest indented nodes run first.
Limit (cost=1243.56..1243.58 rows=10 width=40) (actual time=48
Discussion
Your thoughts matter!
Your input is valuable—be the first to share it!