Sub topic 3
What the Cost Numbers Mean
This chapter assumes you can write SELECT queries with WHERE, JOIN, GROUP BY, and ORDER BY. You should also know what a database index is and how to run SQL in psql or another SQL client.
PostgreSQL's EXPLAIN command shows the plan chosen by the query planner. The planner compares possible plans before running the query. Cost numbers help it choose between them.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;A typical result might contain a line like this:
Index Scan using orders_customer_id_idx on orders
(cost=0.29..12.35 rows=8 width=64)Startup cost and total cost
The two numbers separated by .. are the startup cost and total cost.
- Startup cost is the estimated work before PostgreSQL can return the first row.
- Total cost is the estimated work needed to produce all rows from that plan node.
These values are in PostgreSQL's internal cost units. They are not milliseconds, and they are not a promise about how long the query will take. The planner uses them to compare plans consistently.
A sequential scan often has a small startup cost because it can begin reading the table immediately. Its total cost can be high because it must inspect many pages. An index scan may have more startup work but a lower total cost when it finds only a small number of rows.
The total cost of a parent node includes the work of its child nodes. For example, a sort's cost includes reading the rows it sorts. A join's cost includes producing rows from both inputs.
Estimated rows and width
The rows value is the planner's estimate of how many rows a node will emit. The width value is the estimated average size of each row in bytes.
For example, rows=8 width=64 means the planner expects eight rows, each about 64 bytes wide. These estimates affect later costs. A join that receives many more rows than expected can make a sort, aggregate, or later join much more expensive.
| Value | Meaning | Common use |
|---|---|---|
| `startup..total` | Estimated work before the first row and for all output | Compare alternative plans |
| `rows` | Estimated output row count | Detect possible misestimates |
| `width` | Estimated average row size in bytes | Estimate memory and data movement |
Why the numbers can mislead
The planner's estimates depend on table statistics and configuration values such as the estimated cost of reading sequential or random pages. A plan with a higher cost can sometimes run faster on a particular machine because the hardware, cache state, or data distribution differs from the assumptions.
Do not compare cost numbers from unrelated databases as if they were seconds. Use them mainly to compare plans for the same query and database. Look for large row-estimate errors first. A node estimated at 10 rows but producing 100,000 rows can cause the planner to choose a poor join or sort strategy.
Exercise
An index scan has cost 2.00..18.00, rows=5, and width=40. What do these values mean, and are 18 milliseconds guaranteed?
Show answer
PostgreSQL estimates a small amount of work before the first row, 18 cost units for all five estimated rows, and an average row size of 40 bytes. No time in milliseconds is guaranteed.