A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Table Statistics, Histograms, and Column Statistics

College Depth 90 in the knowledge graph ☐ I know this ☆ Set as goal
493prerequisites beneath it
See this on the map →
Query Cardinality and Selectivity Estimation→
statistics histogram sampling column-stats MFV

Core Idea

Database systems maintain statistics on column distributions: histograms partition values into buckets to represent skewed distributions, most-frequent-value (MFV) lists track high-cardinality values, and sampling estimates statistics without scanning entire tables. The optimizer uses these statistics to estimate selectivity more accurately than uniform distribution assumptions. Regular statistic maintenance is essential as data changes.

Explainer

From your study of cardinality and selectivity estimation, you know the query optimizer needs to predict how many rows a filter or join will produce in order to choose the best execution plan. But where does the optimizer get those predictions? It cannot scan the entire table for every query — that would defeat the purpose of optimization. Instead, the database pre-computes and stores table statistics: compact summaries of column distributions that the optimizer consults during planning.

The simplest statistics are per-column summaries: the number of distinct values, the fraction of NULLs, the minimum and maximum values, and the total row count. These let the optimizer make basic estimates — if a column has 100 distinct values in a 10,000-row table, a filter on a specific value is estimated to return about 100 rows, assuming uniform distribution. But real data is rarely uniform. A `city` column might have 40% of rows in "New York" and 0.1% in "Boise." Assuming uniformity would wildly misestimate both queries.

Histograms solve this by partitioning the column's value range into buckets, each recording a count or frequency. An equi-width histogram divides the range into equal intervals, while an equi-depth (equi-height) histogram adjusts bucket boundaries so each bucket contains roughly the same number of rows — better for skewed data because popular ranges get narrower buckets with more precise estimates. Most-frequent-value (MFV) lists complement histograms by tracking the exact frequencies of the top N most common values. Together, the MFV list handles the peaks and the histogram covers the rest of the distribution.

Because collecting exact statistics requires a full table scan, databases typically use sampling: reading a random subset of pages or rows and extrapolating. PostgreSQL's `ANALYZE` command, for example, samples a configurable number of rows per column to build histograms and MFV lists. This is fast enough to run regularly but introduces sampling error. The critical operational point is that statistics go stale as data is inserted, updated, and deleted. If your table has grown 10x since the last ANALYZE, the optimizer is planning queries against an outdated picture of the data. Most databases offer auto-analyze features, but high-churn tables may need manual intervention. When a query plan suddenly degrades, stale statistics are one of the first things to check.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Time and Space Complexity → Binary Search → Binary Search Trees → B-Tree Indexes → Query Optimization → Query Cardinality and Selectivity Estimation → Table Statistics, Histograms, and Column Statistics

Longest path: 91 steps · 493 total prerequisite topics

Prerequisites (1)

Leads To (0)

No topics depend on this one yet.