reading EXPLAIN
a tree you read from the inside out. cost, rows, width, and what each number actually means.
- address
- everything.execution.reading_explain
- part
- part 9, how a query runs
- chapter
- chapter 4 of 13
- reading time
- 9 min
EXPLAIN prints a tree of plan nodes, and you read it from the inside out: the most indented lines run first, and rows flow up to the top. every node line carries four estimates: startup cost, total cost, rows and width.
none of them are measured. they're the planner's guesses, in made-up units, and that's fine once you know what each one means.
the picture
you know the wall of arrows EXPLAIN gives you, where the first line is the last thing that happens? kinda cursed. it reads fine once you stop reading it top to bottom.
think of a recipe with sub-recipes. the card says "plate the 5 best dishes", and under it, indented, "sort the dishes", and under that "cook each customer's order", and under that two lines: "chop the onions" and "boil the stock". nobody starts at the plating. you start at the most indented line (the leaves), and each step hands its result up to the line above it (its parent).
here's the query this page takes apart: the five customers in paris with the most orders.
EXPLAIN
SELECT c.name, count(*) AS orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE c.city = 'Paris'
GROUP BY c.id
ORDER BY orders DESC, c.id
LIMIT 5;seven nodes. every -> is a child of the line above it with less indentation, and the lines under a node with no arrow (Filter:, Hash Cond:, Sort Key:) are that node's details. now as a tree, with the text beside it:
- 1SELECT c.name, count(*) AS orders
- 2FROM customers AS c
- 3JOIN orders AS o ON o.customer_id = c.id
- 4WHERE c.city = 'Paris'
- 5GROUP BY c.id
- 6ORDER BY orders DESC, c.id
- 7LIMIT 5
- Limit (cost=262.54..262.55 rows=5 width=24) (actual rows=5.00 loops=1)
- -> Sort (cost=262.54..262.70 rows=66 width=24) (actual rows=5.00 loops=1)
- Sort Key: (count(*)) DESC, c.id
- Sort Method: top-N heapsort Memory: 17kB
- -> HashAggregate (cost=260.78..261.44 rows=66 width=24) (actual rows=66.00 loops=1)
- Group Key: c.id
- Batches: 1 Memory Usage: 32kB
- -> Hash Join (cost=49.83..259.13 rows=330 width=16) (actual rows=369.00 loops=1)
- Hash Cond: (o.customer_id = c.id)
- -> Seq Scan on orders o (cost=0.00..183.00 rows=10000 width=4) (actual rows=10000.00 loops=1)
- -> Hash (cost=49.00..49.00 rows=66 width=16) (actual rows=66.00 loops=1)
- Buckets: 1024 Batches: 1 Memory Usage: 7kB
- -> Seq Scan on customers c (cost=0.00..49.00 rows=66 width=16) (actual rows=66.00 loops=1)
- Filter: (city = 'Paris'::text)
- Rows Removed by Filter: 1934
#1start here. the Seq Scan on customers reads 24 pages, 2,000 rows, and its Filter keeps the 66 in paris. cost 0.00..49.00: nothing to do before its first row, 49 to finish.
so the order it really runs in is the reverse of the order it's printed in: scan customers, hash them, stream orders past them, group, sort, cut. (the last two are the logical order you already know, GROUP BY before ORDER BY before LIMIT, showing through.)
Limit (cost=262.54..262.55 rows=5 width=24)
-> Sort (cost=262.54..262.70 rows=66 width=24)
...four numbers per line
every node line ends in (cost=startup..total rows=n width=w). one at a time:
total cost is what the planner thinks it costs to run this node to the end, including all of its children. the unit is arbitrary: reading one page in order costs seq_page_cost, which is 1.0, and everything else is priced relative to that. it isn't milliseconds, and it never will be.
it's not magic either. here's the scan of customers, priced by hand from the catalog:
EXPLAIN SELECT * FROM customers WHERE city = 'Paris';
SELECT relpages, reltuples,
relpages * current_setting('seq_page_cost')::float8 AS page_cost,
reltuples * current_setting('cpu_tuple_cost')::float8 AS row_cost,
reltuples * current_setting('cpu_operator_cost')::float8 AS filter_cost
FROM pg_class
WHERE relname = 'customers';| relpagesint4 | reltuplesfloat4 | page_costfloat8 | row_costfloat8 | filter_costfloat8 |
|---|---|---|---|---|
| 24 | 2000 | 24 | 20 | 5 |
pg_class.relpages, rows from reltuples, each times a cost setting. the filter costs a quarter of what handling a row does.every node has a formula like that. the rest of them, and the knobs, are the cost model.
startup cost is the part of the total spent before the node can return its first row. a scan can hand over its first row right away, so its startup cost is 0.00. a Sort can't: the smallest row might be the last one it reads. so a Sort's startup cost is everything below it plus the sorting, and only the handing out is left for the rest. drawn on one axis, it looks like this:
that's why startup cost matters. with a LIMIT on top, postgres prefers plans that can stop early, and a plan full of Sorts and Hashes can't. (the iterator model is about exactly that.)
rows is how many rows the node expects to return, not to read. the Seq Scan on customers reads 2,000 rows and expects to return 66. the estimate comes from statistics about the column: Paris is one of its most common values, and ANALYZE wrote down how common:
SELECT most_common_freqs[array_position(most_common_vals::text::text[], 'Paris')] AS paris
FROM pg_stats
WHERE tablename = 'customers' AND attname = 'city';| parisfloat4 |
|---|
| 0.033 |
0.033 of 2,000 rows is 66. how the planner gets to that number for any WHERE is estimating rows. for a node that runs more than once, it's per run (loops).
width is the average size of one returned row, in bytes, counting only the columns the nodes above still need. not the table's row size:
EXPLAIN SELECT * FROM customers;
EXPLAIN SELECT id FROM customers;same scan, same 44.00, width 61 versus 4. in the paris plan, customers comes out 16 bytes wide (an id of 4 and a name of 12 on average) and orders comes out 4 wide, since all the join needs from orders is customer_id. width times rows is roughly the memory a Hash or a Sort is going to want, which is why the planner tracks it.
rows=330. what is 330?when you don't care about cost
half the time you want the shape, not the numbers. COSTS OFF drops them:
EXPLAIN (COSTS OFF)
SELECT c.name, count(*) AS orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE c.city = 'Paris'
GROUP BY c.id
ORDER BY orders DESC, c.id
LIMIT 5;that's the form most of this part uses. the other options worth knowing: ANALYZE runs the query and adds what really happened (the next chapter), VERBOSE adds the output columns of every node, SETTINGS lists the planner settings you changed, and FORMAT JSON prints the same tree as json, which is what tools like this page's figures read.
one level deeperthe Limit's cost, worked out
a Limit doesn't do anything itself, it stops early. so its cost is a slice of its child's: the child's startup cost, plus the fraction of the child's remaining cost that it will consume. it takes 5 of the 66 rows the Sort expects, so:
262.54 + (262.70 − 262.54) × 5 / 66 = 262.55
that interpolation assumes rows come out evenly over the child's run, which is fine for a scan and generous for a Sort, but a Sort's startup cost already holds nearly everything, so the slice is tiny either way.
keylines
- read EXPLAIN inside out: the most indented nodes run first, rows flow up.
- cost=startup..total, in units where one sequential page read is 1.0.
- every node's cost includes its children, so the top line is the whole query.
- rows is what a node returns (per loop), width is the average bytes per row it returns.
- a high startup cost means "has to read everything first": Sort, Hash, aggregates.