Sub topic 12
How Planner Statistics Drive Estimates
Assume you can write SELECT queries with WHERE, JOIN, GROUP BY, and ORDER BY, and that you know what a database index is. This chapter uses PostgreSQL.
Before choosing a scan or join, the planner estimates how many rows each step will produce. These estimates come mainly from statistics collected about table columns. The planner uses them to compare possible plans. A wrong estimate can lead to a poor choice even when suitable indexes exist.
What `ANALYZE` collects
ANALYZE reads a sample of table rows and stores summary information. It does not record every row. PostgreSQL keeps the summaries in system views such as pg_stats.
ANALYZE orders;
SELECT attname, n_distinct, null_frac, correlation
FROM pg_stats
WHERE tablename = 'orders';Important statistics include:
n_distinctestimates how many different values a column contains.null_fracestimates the fraction of rows whose value isNULL.- A histogram describes how non-common values are distributed across ranges.
- Most-common-value data records values that appear unusually often.
correlationdescribes how column values relate to the physical order of table rows.
For example, if customer_id = 42 matches only a few rows, an index scan may be attractive. If it matches half the table, a sequential scan may be cheaper. The planner needs statistics to make that distinction.
ANALYZE normally runs automatically through PostgreSQL's autovacuum process. Run it manually after a large data load, a bulk update, or a major change in the distribution of values.
Why estimates become wrong
A column may have a skewed distribution. A few customers might own most orders, while many customers have only a few. A simple average cannot represent that pattern accurately. Most-common-value statistics help, but they are still based on a sample and a limited statistics target.
Estimates can also fail when predicates use related columns. Suppose nearly every row with city = 'Boston' also has zip_code = '02108'. If the planner treats the two filters as independent, it may estimate too few rows.
Extended statistics describe relationships between columns:
CREATE STATISTICS orders_city_zip_stats (dependencies)
ON city, zip_code FROM orders;
ANALYZE orders;The dependencies kind tells PostgreSQL that values in one column can help predict values in another. Other extended-statistics kinds can describe distinct-value combinations or multi-column value frequencies.
Statistics targets and trade-offs
PostgreSQL limits how much detail it stores for each column. A higher statistics target can improve estimates for columns with complicated distributions, but ANALYZE takes more sampling work and the catalog stores more data.
ALTER TABLE orders
ALTER COLUMN customer_id SET STATISTICS 500;
ANALYZE orders;Increase the target for a problem column rather than changing every column. Confirm the improvement by comparing the estimated rows with the actual rows in EXPLAIN (ANALYZE).
Exercise
After loading millions of new rows into orders, a plan estimates 10 rows but returns 200,000. What should you try first?
Show answer
Run ANALYZE orders, then inspect the plan again. If the estimate remains wrong, check the column distribution, raise the statistics target for the relevant column, or add extended statistics for correlated predicates.