Sub topic 10
Comparing Estimated and Actual Rows
Assume you already know how to write SELECT, WHERE, JOIN, GROUP BY, and ORDER BY queries. This chapter uses PostgreSQL and assumes you can run SQL in psql or another SQL client.
A query plan contains two kinds of row counts:
- Estimated rows are the planner's prediction before execution.
- Actual rows are the rows that a plan node produced during execution.
Compare these values to find estimation errors. A bad estimate can cause PostgreSQL to choose an inefficient scan or join.
Reading the two row counts
Run EXPLAIN ANALYZE to see both values:
EXPLAIN (ANALYZE)
SELECT *
FROM orders
WHERE customer_id = 42;A simplified result might look like this:
Index Scan using orders_customer_id_idx on orders
(cost=0.42..18.50 rows=12 width=96)
(actual time=0.030..0.080 rows=240 loops=1)The estimated row count is 12. The actual row count is 240. PostgreSQL expected 12 rows but found 240, so the estimate was off by a factor of 20.
The other values have different meanings:
costis the planner's internal cost range. It is not elapsed time.widthis the estimated average size of each output row in bytes.actual timereports startup time and total time in milliseconds.loopsreports how many times the node ran.
When loops is greater than 1, inspect the relationship between rows and loops. PostgreSQL commonly reports rows produced per loop. For example, rows=5 loops=100 means about 500 rows were produced in total.
| Value | Meaning | Main use |
|---|---|---|
| Estimated rows | Planner prediction | Explaining the chosen plan |
| Actual rows | Rows produced during execution | Checking the prediction |
| Loops | Number of node executions | Finding repeated work |
| Width | Estimated row size | Understanding memory and I/O costs |
Why estimates become wrong
PostgreSQL builds estimates from planner statistics. It records information such as how many distinct values a column has, how often it is null, and which values are common.
Estimates can become stale after many inserts, updates, or deletes. A table may also contain relationships between columns that simple statistics do not capture. For example, city and zip_code are related, but PostgreSQL might initially estimate them as independent filters.
A large difference between estimated and actual rows is a warning, not automatic proof that the query is slow. It matters when the error changes a plan choice.
Confirming and fixing a misestimate
First, compare rows= with actual rows= at every important node, especially before joins. A large error early in the tree can multiply through later nodes.
Refresh statistics with ANALYZE:
ANALYZE orders;
EXPLAIN (ANALYZE)
SELECT *
FROM orders
WHERE customer_id = 42;You can inspect statistics for a table with pg_stats:
SELECT attname, n_distinct, null_frac
FROM pg_stats
WHERE tablename = 'orders';If estimates remain wrong because columns are correlated, create extended statistics:
CREATE STATISTICS orders_city_zip_stats (dependencies)
ON city, zip_code FROM orders;
ANALYZE orders;Other fixes include rewriting a predicate, checking data types, or adding an appropriate index. Do not add an index only because a sequential scan appears. A sequential scan can be correct when a query needs a large part of a table.
Exercise
A plan estimates 100 rows, but the node reports actual rows of 10,000 with loops=1. What does this suggest, and what should you check first?
Show answer
The planner underestimates the result by a factor of 100. Check whether statistics are stale by running ANALYZE, then inspect pg_stats and the query's predicates for skew or column correlation.