Sub topic 11
Keeping statistics useful for the optimizer
This chapter assumes you know basic SQL, can run queries in a Databricks workspace, and understand that Delta tables store data in Parquet files. You should also have read the chapters on query plans and data skipping.
Why the optimizer needs statistics
Spark's cost-based optimizer chooses join order and join type by estimating how many rows each step produces. It builds those estimates from statistics: row counts, table size, and per-column values such as distinct counts, minimums, maximums, and null counts.
If the statistics are missing or stale, the estimates are wrong. A table that grew from 1 million to 500 million rows may still look small to the optimizer. It might then pick a broadcast join for a table that no longer fits in memory, or put tables in a poor join order.
Two kinds of statistics
Delta tables hold two different sets of statistics. They do different jobs.
| Kind | Where it lives | What uses it |
|---|---|---|
| File-level statistics (min, max, null count per column per file) | Delta transaction log | Data skipping, which decides which files to read |
| Table and column statistics (row count, distinct count, size) | Metastore | The cost-based optimizer, which decides join order and join type |
This chapter is about the second kind. File-level statistics are collected automatically as data is written, usually for the first 32 columns of a table.
Collecting statistics with ANALYZE TABLE
Run ANALYZE TABLE to compute table and column statistics:
ANALYZE TABLE orders COMPUTE STATISTICS FOR ALL COLUMNS;This scans the table, so it costs compute time on large tables. You can limit the work to the columns that matter, such as join keys and filter columns:
ANALYZE TABLE orders COMPUTE STATISTICS FOR COLUMNS customer_id, order_date;You can inspect the result with DESCRIBE EXTENDED orders for table-level figures, or DESCRIBE EXTENDED orders customer_id for one column.
When statistics go stale
Statistics are a snapshot. They do not update on their own each time data changes, unless you use a feature that maintains them for you (see the chapter on predictive optimization). Refresh them after:
- A large load, delete, or
MERGEthat changes the row count significantly. - A change in the data distribution, such as a new region dominating a column.
- A plan that suddenly gets worse with no code change.
The EXPLAIN output can show the optimizer's estimates. If the estimated row count is far from the real one, suspect missing or stale statistics.
Common errors
- Assuming
OPTIMIZErefreshes optimizer statistics. It compacts files, but it is a separate operation fromANALYZE TABLE. - Running
ANALYZE TABLEonce at creation and never again on a table that keeps growing. - Collecting statistics for every column on a very wide table, which wastes compute.
- Adding a
BROADCASThint to hide a bad estimate. The hint overrides the optimizer, but the stale statistics still affect other queries.
Exercise
A query joins a large orders table with customers. After a big nightly load, the plan changed from a broadcast join to a sort-merge join, and the EXPLAIN output shows an estimated row count for orders that is much lower than the real count. What should you run first, and why?
Show answer
Run ANALYZE TABLE orders COMPUTE STATISTICS FOR ALL COLUMNS (or for the join and filter columns). The estimate is far below the real size, so the stored statistics are stale. Refreshing them lets the optimizer cost the join correctly. Then compare the new plan with EXPLAIN to confirm the estimate is closer to reality.