Part 2 of 5 · 1 min read
Statistics: what the optimizer actually knows about the data
The optimizer relies on periodically collected summaries of the data, not the live data itself.
Think of a store manager estimating how busy a specific day will be using last month's sales summary, rather than walking the entire store floor counting customers in real time.
In plain words: the optimizer does not examine a table's actual live data every time it plans a query; doing so would be far too slow. Instead, it relies on statistics: periodically collected summaries, such as the total row count, the number of distinct values in a column, and a rough distribution of those values. These statistics are usually refreshed on a schedule or after significant changes, not continuously, so they can drift out of date as the real data changes.
A worked example. The optimizer estimating how many orders rows match customer_id = 42 relies on a stored statistic about how many distinct customer_id values exist and how evenly rows are spread among them, not a live count.
The key lines. No new SQL; statistics are typically refreshed with a dedicated maintenance command, such as ANALYZE, run periodically or after major data changes.
Statistics (optimizer)
Statistics, for the optimizer, are periodically collected summaries of a table's data, such as row counts and the distribution of distinct values, used to estimate plan costs without scanning the live data each time.
Example: a store manager's monthly sales summary, used instead of a live floor count, is the statistics equivalent for planning.