Skewed Data: The Average Row Count Is Not the Row Count for Your Value
Real data is lumpy. If one customer owns 40% of the orders and the other thousands own the rest, then customer_id = ? has wildly different selectivity depending on which customer you ask about. The most-common-values list exists to cover exactly the top values; beyond that list the planner falls back on an average that fits almost nobody. Skew is why the same query with a different parameter can take 3 milliseconds or 30 seconds, and why testing with a typical value hides the problem.
Questions this Concept answers
- Why can the same query be fast for one parameter value and slow for another?
Understanding Data Skew and Its Impact on PostgreSQL Query Performance
This lesson explains data skew: when a few values in a column appear far more often than the rest. It shows how PostgreSQL keeps statistics, such as a list of most common values and a histogram, and …