Sub topic 3
Comparing estimated and actual rows
This chapter assumes you can write SELECT queries with WHERE, JOIN, GROUP BY and ORDER BY, that you know what an index is, and that you can run EXPLAIN ANALYZE in psql. Examples use PostgreSQL.
Comparing estimated and actual rows
Every node in a plan shows rows= as the planner's estimate. With ANALYZE, the node also shows actual ... rows=, the count it really produced.
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE customer_id = 42;Index Scan using orders_customer_id_idx on orders
(cost=0.43..8.45 rows=1 width=64) (actual time=0.02..0.03 rows=3 loops=1)The planner expected 1 row and got 3. That gap is harmless here. A gap of 100x or more is a misestimate, and it often leads to a poor join method.
> Note: for nodes inside a loop, actual rows is the average per loop. Multiply by loops to get the total.
Reading BUFFERS output
The BUFFERS option adds counts of 8 KB pages each node touched:
shared hit=Nmeans N pages came from the cache.read=Nmeans N pages came from disk.
A node with many read pages is slow on a cold cache, even when its row estimate is correct.
How planner statistics drive estimates
The planner does not read the table to estimate rows. It uses statistics that ANALYZE collects and stores in the pg_stats view. These include distinct-value counts and the most common values for each column.
| Statistic | Used for |
|---|---|
| `n_distinct` | Estimating how many rows match an equality test |
| `null_frac` | Estimating how many rows have NULL |
| Most common values | Estimating rows for frequent values |
Stale statistics produce misestimates. Run ANALYZE orders; after large data changes.
Finding the node that costs the most
Read the plan from the innermost nodes outward. Actual time for a parent node includes its children. Subtract the children's time to find the cost of the node itself.
Fixing a slow plan: indexes, statistics and rewrites
Match the fix to the problem:
- A large scan with few matching rows usually needs an index.
- A misestimate usually needs fresh statistics or extended statistics.
- A query that forces unnecessary work usually needs a rewrite.
CREATE INDEX orders_customer_id_idx ON orders (customer_id);
ANALYZE orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE customer_id = 42;Run the EXPLAIN again to confirm the change.
How EXPLAIN differs in MySQL, SQL Server and others
- MySQL 8.0.18 and later supports
EXPLAIN ANALYZE, which shows actual rows and timing per node. - SQL Server shows estimated and actual row counts in the actual execution plan, or in XML from
SET STATISTICS XML ON. - SQLite has
EXPLAIN QUERY PLAN, which shows scan and join strategies but not actual row counts.
The idea is the same everywhere: compare what the optimizer expected with what happened.
Exercise
Exercise
A node reports rows=1 but actual rows=50000. What is the likely cause, and what is one fix?
Show answer
The planner's statistics are stale or missing for the filtered column, so it expected 1 row and may have chosen a nested loop join. Run ANALYZE on the table. If the filter uses two correlated columns, create extended statistics with CREATE STATISTICS and then run ANALYZE.