Which node in an EXPLAIN plan is actually slow? Reading actual time at face value gets it wrong, and I only noticed while parsing plans into a tree for a browser-only EXPLAIN visualizer (nothing you paste leaves the page).
Parsing the text back into a tree is the easy half. Here are the three things that bit me.
Trap 1: actual time includes the children
In EXPLAIN (ANALYZE), actual time=0.010..40.000 includes the time the node spent waiting on its children. If you turn that into a percentage, the root (Sort, Limit) is always near 100% and tells you nothing.
A node's own time is:
own = (time per loop × loops) − Σ(child time per loop × child loops)
A real PostgreSQL 16 plan (trimmed):
Merge Join (… ) (actual time=3.426..63.571 rows=18000 loops=1)
-> Index Scan using idx_orders_user on orders o (… ) (actual time=0.010..44.488 rows=99996 loops=1)
-> Sort (… ) (actual time=2.873..3.233 rows=3600 loops=1)
-> Seq Scan on users u (… ) (actual time=0.018..2.266 rows=3600 loops=1)
Execution Time: 64.868 ms
Read raw, the Merge Join is "98% of the query" (63.6 of 64.9 ms). Subtract its children and its own time is 63.57 − 44.49 − 3.23 ≈ 15.9 ms (25%). The Index Scan is the real hot spot at 44.5 ms (70%), which is a very different thing to go fix.
Trap 2: loops
The inner side of a Nested Loop reports per-loop time: actual time=0.010..0.050 loops=2000. If you forget to multiply by loops, the node that ran 2,000 times and dominates the query looks like the lightest one.
(Row counts are per loop too, so compare estimated and actual rows without multiplying. I got this backwards at first and flagged every nested-loop inner side as a bad estimate.)
Trap 2b: under a Gather, don't multiply by loops
Edit: a reader pointed out that the loops multiplication breaks under parallel plans. They're right.
Below a Gather / Gather Merge, loops is the number of participants (workers plus the leader), and they all run at the same time. Multiplying per-loop time by loops adds up time that overlapped. On a parallel GROUP BY over 3M rows (495 ms total) that gave the Gather an own time of about −957 ms and the Seq Scan 833 ms.
The fix: the node right under the Gather runs once per participant, so its loops is the participant count N. For everything below it, use per-loop time × loops / N. For that first node it's just the per-loop time. An inner Nested Loop side that ran 4,000 times across 2 participants counts as 2,000 iterations of wall-clock work.
Trap 3: the text tree is defined by the -> column
Hash Join (cost=…) (actual time=…)
Hash Cond: (o.user_id = u.id)
-> Seq Scan on orders o (cost=…)
Filter: (total > 100)
-> Hash (cost=…)
-> Seq Scan on users u (cost=…)
The root is the first line without an arrow, and a child is any -> whose column is to the right of its parent's. Push columns on a stack and pop everything at or beyond the new arrow's column. Indentation of the detail lines varies per node, so don't use it.
Things I only found by running real output through the parser:
-
CTE t,InitPlan 1,SubPlan 2are headings, not nodes with a cost, and their subplans hang below them. - A trailing
Planning:/Buffers:/JIT:block must not be glued onto the last node's details. -
Bitmap Index Scan on idx_orders_user: the name afteronis an index, not a table. - JSON output names nodes differently from text (
Aggregate+Strategy: HashedisHashAggregatein text), so normalize both.
MySQL and MariaDB
EXPLAIN FORMAT=TREE has the same arrows, but the root has one too, and Filter: (…) / Limit: 20 row(s) lines are the node type plus its detail. MariaDB's ANALYZE FORMAT=JSON is different again: the per-table times are already exclusive, so subtracting children would be wrong.
Try it
The tool is here, runs entirely in your browser, and reads PostgreSQL (text/JSON), MySQL, MariaDB and SQLite plans: EXPLAIN Visualizer
Top comments (3)
The loops multiply breaks under Gather. On PG17 a parallel GROUP BY over 3M rows (495 ms total) gave the Gather an own time of -957 ms and the Seq Scan 833 ms, because loops=3 counts the workers, which run at the same time. Below a Gather I'd use the per-loop time. Does the tool special-case parallel nodes?
Yeah, you're right, that was a bug. Under a Gather I was multiplying by loops, which just adds up time the workers spent in parallel. Fixed it: I divide by the number of participants now (the loops of the node right under the Gather), so the Seq Scan shows its per-loop time and the Gather isn't negative anymore. Tried it on a plan shaped like yours (Gather > Partial HashAggregate > Parallel Seq Scan, loops=3) and it looks sane.
I'll also add a note about this to the post.
the merge join reads as 98% of the query. subtract the children and the index scan is the real hot spot.