Sub topic 2
Reading a Plan from the Inside Out
Assume you can write SELECT queries with WHERE, JOIN, GROUP BY, and ORDER BY. You should also know that an index is a data structure that helps a database find rows without reading a whole table. The examples use PostgreSQL.
A plan is a tree
When PostgreSQL receives a query, its planner chooses a sequence of operations called a query plan. The plan is a tree. The top node produces the final result, and its child nodes produce the input that the parent needs.
Run EXPLAIN to see the planned tree without running the query:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;A plan might look like this:
Index Scan using orders_customer_id_idx on orders
Index Cond: (customer_id = 42)The Index Scan is both the top node and the only node here. It uses the index to find matching rows.
For a join, the tree has more levels:
Limit
Sort
HashAggregate
Hash Join
Seq Scan on orders
Hash
Seq Scan on customersRead this tree from the bottom upward:
- PostgreSQL reads rows from
orders. - PostgreSQL reads rows from
customers. - It builds a hash table from the customer rows.
- It joins the two inputs.
- It groups the joined rows.
- It sorts the groups.
- It keeps only the requested number of rows.
The parent node consumes rows from its children. This explains why a problem deep in the tree can make the final query slow.
What each plan line tells you
A node usually includes its operation, estimated startup cost, estimated total cost, estimated row count, and estimated row width. For example:
Seq Scan on orders (cost=0.00..1850.00 rows=12000 width=64)| Value | Meaning |
|---|---|
| `cost=0.00..1850.00` | Planner units for startup and total work |
| `rows=12000` | Estimated number of rows produced |
| `width=64` | Estimated average size of each output row, in bytes |
The cost is not milliseconds. PostgreSQL uses it to compare alternative plans. A plan with a lower estimated cost is usually preferred, even if the number has no direct time meaning.
The rows value is especially useful. It describes the output of that node, not necessarily the number of rows stored in the table. A filter can scan many rows and output only a few.
Read parents by inspecting their children
When you see a join or aggregate, first inspect the nodes below it. Ask:
- How many rows does each child produce?
- Is one child much larger than expected?
- Does a filter happen before or after a join?
- Does a sort receive more rows than necessary?
The node at the top is often not the cause of the problem. A Sort may be slow because an earlier join produced millions of rows. A HashAggregate may use too much memory because a filter was not selective.
Practice
Exercise
In a plan with Limit above Sort, above Seq Scan, which operation supplies rows to the sort, and where should you look first if the sort is slow?
Show answer
The sequential scan supplies rows to the sort. First check how many rows the scan produces and whether a filter or index could reduce that input.