opyeb SQLPlan

Guides · Plan basics

How to read a plan tree: indentation, loops and self time

Updated 28 Nov 2026 · 7 min read

A plan is a tree of operators. PostgreSQL prints the root first, then its children indented, then their children further indented, with a -> marker on lines that are children. MySQL uses the same visual convention with deeper indentation. The indentation is the whole structure: a line's parent is the nearest line above it with less indentation.

Execution flows the other way. A sequential scan at the bottom produces rows, a hash join above it consumes two inputs, a sort above that orders the result, and the root returns it to the client. Understanding both directions matters: the printed order tells you what the query returns, and the execution order tells you which node had to do the work first.

LineMeaningWhat to look at
SortRoot, orders the final resultSort Method, Disk, memory
Hash JoinCombines two inputs on a hashHash Cond, batches, build side size
Seq Scan on ordersLeaf reading the whole tableRows Removed, Filter, buffers
HashBuilds the hash tableBuckets, Batches, Memory Usage
Seq Scan on usersSecond leafRows, filter, cost

Loops change the arithmetic

Each PostgreSQL node reports actual time and rows per loop. For a node executed once, the printed time is the total. For a node on the inner side of a nested loop, the printed time is one pass; the total is that time multiplied by loops. The same applies to rows: rows=1 loops=1000 means one row per pass and 1,000 rows overall.

This is the single most common misreading of a plan. A scan line that says 4.190 ms looks cheap next to a 4,200 ms query, but with 1,000 loops it accounts for 4,190 ms. In this analyzer, the summary multiplies before subtracting, and the row shows the loop count next to the node so the multiplication is visible.

Self time versus total time

A parent's actual total includes the time its children spent producing rows. If you optimise the parent while the child is the bottleneck, nothing improves. Self time is the parent's total minus the sum of its children's totals, with each child multiplied by its own loops. It is an approximation, because a parent can overlap work with a child in some operators, but it is far better than reading the top line alone.

Consider a hash join with two children: a scan at 180 ms and a hash build at 280 ms, with a total of 300 ms. Its self time is 300 − 180 − 280 = negative, which the analyzer clamps to zero. That means the join itself is cheap and the time is in the children. A negative value is normal and is a signal to look lower in the tree.

Sibling order is not arbitrary

For a nested loop, the first child is the outer side and the second is the inner side. The outer side is scanned once; the inner side is rescanned for each outer row. For a hash join, the first child is usually the build side and the second is the probe side, though the planner may choose the reverse. Knowing which side is which explains the loop counts and where an index helps.

In a merge join, both children must produce rows in the join key order, which may require sorts or index scans. If a child has a Sort node above it, the merge join is paying for ordering. If both children are index scans on the join key, the merge join can stream without sorting, which is often the cheapest large join.

Reading a tree in practice

  1. Start at the root and read down to identify the shape: scans, joins, sorts, aggregates.
  2. Find the node with the largest self time; that is where the time is spent.
  3. Check loops on every node below a nested loop; multiply before comparing.
  4. Compare estimated rows with actual rows on each node to find where the planner lost track.
  5. Read the detail lines — filter, join condition, sort method, buffers — on the slow nodes.
  6. Change one thing and measure again; the tree shape may change, so re-read it.

The analyzer automates the arithmetic but not the interpretation. It can tell you that a scan has 1,000 loops and 4 ms per loop, that a sort spilled 48 MB to disk, and that an estimate was off by a factor of 200. Deciding whether to add an index, rewrite the predicate or raise work_mem still depends on the query and the data.

Read the tree twice: once top-down for shape, once bottom-up for work. Then read the loops, because they change every number.

Sources

Questions

Do children execute before their parent?

Yes. A parent pulls rows from its children; the leaves produce the base rows. The tree is printed with the root at the top, which is the opposite of the execution order. Read the shape top-down for what the query returns, and bottom-up for where the work starts.

What does a loop count above one mean?

The node ran that many times, usually because it is the inner side of a nested loop that was rescanned for each outer row. Actual time and rows are per loop, so total work is the value multiplied by loops. A node with 1,000 loops and 4 ms each costs 4 seconds, even though its line says 4 ms.

Why is my node's total less than its children's total?

Parent actual time includes the time spent in children, and children may have different loop counts. A parent with one loop and children with 1,000 loops can show a smaller number on the parent line. Multiply each value by its own loops before comparing, or use the self-time calculation.

The analyzer keeps the printed tree and adds a self-time bar to every row.

Open the analyzer

Written against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.

Back to the guides