Quick revision
The short answer
The planner assumes columns are independent and multiplies their selectivities, but city = 'Berlin' almost implies country = 'DE', so the product is far too small and it estimates 12 rows instead of 400,000. With 12 rows, a nested loop looks cheap; with 400,000 it runs for minutes. We fix the estimate with extended statistics, CREATE STATISTICS ... (dependencies, mcv) ON country, city, followed by ANALYZE, and confirm with EXPLAIN ANALYZE that estimates match and the join becomes a hash join.
What to remember
In this answer, we saw that PostgreSQL multiplies selectivities as if columns were independent, so a filter on country and city, where city almost determines country, was estimated at 12 rows instead of 400,000, and the planner chose a nested loop that ran for minutes. Extended statistics with dependencies and mcv, followed by ANALYZE, gave a correct estimate and a hash join. We also saw raising the statistics target and rewriting the query as other levers, that hints come from an extension and are a last resort, and that MySQL 8 has histograms but no multi-column dependency statistics.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The planner assumes columns are independent and multiplies their selectivities, but city = 'Berlin' almost implies country = 'DE', so the product is far too small and it estimates 12 rows instead of 400,000. With 12 rows, a nested loop looks cheap; with 400,000 it runs for minutes. We fix the estimate with extended statistics, CREATE STATISTICS ... (dependencies, mcv) ON country, city, followed by ANALYZE, and confirm with EXPLAIN ANALYZE that estimates match and the join becomes a hash join.
Why this question is asked
A wrong row estimate is the root cause of most bad query plans. The query is fine, the indexes are fine, but the planner believes something false about the data and picks a plan that would be excellent if it were true. Interviewers ask this case because it tests whether we read EXPLAIN ANALYZE output for estimate errors and know the tool PostgreSQL provides for correlated columns. In this answer, we will recap how the planner estimates a combined filter, see why it is wrong here, create extended statistics, verify the new plan, and look at the other levers and at MySQL.
1A Quick Recap of How Estimates Are Built
For each column, ANALYZE stores statistics in pg_stats: most common values with their frequencies, a histogram, and the number of distinct values. For WHERE country = 'DE', the planner looks up how often DE appears, say 10 percent. For WHERE city = 'Berlin', say 0.4 percent. When the query has both conditions joined by AND, the planner by default assumes the columns are independent, like two dice, and multiplies the two fractions.
Notice that two mistakes compound here. Independence alone counts the overlap as if it were rare: because Berlin implies Germany, the true selectivity of the pair is the selectivity of city alone, and multiplying by 0.10 makes the estimate ten times too small. If, in addition, the per-column statistics underestimate Berlin, which happens when a skewed value is missing from the sampled most-common list, the error grows to the four orders of magnitude we see. Extended statistics address both at once, because their mcv list stores the frequency of the pair (DE, Berlin) itself.
2Why the Wrong Estimate Picks a Slow Join
The query joins addresses to another table, say orders. With an estimate of 12 address rows, the planner reasons that the cheapest plan is a nested loop: for each of the 12 rows, look up matching orders with an index. Twelve index lookups take microseconds. A hash join would first build a hash table of one side, which looks like wasted work for 12 rows.
But the nested loop actually runs 400,000 times, each with an index lookup that may read pages from disk. A plan that should take a second runs for minutes. The estimate error, not the join method, is the bug.
Notice how to read this: compare rows=12, the estimate, with actual rows=400000 on the same node. A gap of several orders of magnitude on a node that feeds a join is the signal to look at statistics.
3Fixing the Estimate With Extended Statistics
Extended statistics are statistics about a group of columns, collected by ANALYZE like ordinary ones. The dependencies kind measures functional dependency, how strongly one column determines another. The mcv kind stores the most common combinations of values with their frequencies, which is the most precise for pairs like (DE, Berlin). There is also ndistinct for the number of distinct combinations, which helps GROUP BY estimates.
- Run EXPLAIN (ANALYZE, BUFFERS)on a copy with real data
- Estimated rows far from actual?YesFind the first node where they divergeNoLook elsewhere: indexes, I/O
- Filter on related columns?YesCREATE STATISTICS (dependencies, mcv), ANALYZENoCheck single-column stats, raise target
- Re-run EXPLAIN ANALYZEestimates agree, join method changes
4 steps in order. Play them, or step through them one at a time.
So the estimate is now close to reality, and with 400,000 rows the planner correctly prefers a hash join. In the dependency output, the numbers refer to the column positions in the statistics definition (check your version for the exact display), and a value near 1 means one column almost fully determines the other. Extended statistics are only used once ANALYZE has run after they are created, which is why the ANALYZE step matters.
4Other Levers, and Why Not Hints First
If one column is very skewed, for example a few cities hold most rows, we can raise its statistics target so ANALYZE keeps more most-common values: ALTER TABLE addresses ALTER COLUMN city SET STATISTICS 1000, then ANALYZE. We can also restructure the query, for example dropping the redundant country condition if city already implies it, or splitting a complex query so a part is materialised first.
Planner hints, instructions that force a join method or order, are tempting but should not be the first fix. PostgreSQL has no hints built in; the pg_hint_plan extension adds them. A hint freezes one plan even as data grows and changes, while correct statistics let the planner adapt. Hints are a last resort for a proven case where statistics cannot express the data.
5What About MySQL?
MySQL 8 can build histograms with ANALYZE TABLE addresses UPDATE HISTOGRAM ON city WITH 256 BUCKETS, which helps with skewed single columns that have no index. It has no multi-column dependency statistics like PostgreSQL's CREATE STATISTICS, so correlated predicates are still estimated with independence assumptions, though index dives on a composite index can give better estimates for indexed combinations. Check your version, because the optimizer's statistics features keep changing.
6Follow-up questions
Why do bad row estimates matter most for join order?
Because the size of each intermediate result decides the cost of everything after it. The planner chooses which table to start with, which to join next, and which join method to use, based on how many rows it expects at each step. If the first step is estimated at 12 rows, the planner happily nests loops and puts expensive lookups on the inner side; when the step really returns 400,000 rows, every later step is multiplied by that error. An error at a leaf of the plan grows as it moves up through the joins, so one wrong estimate can turn a one-second query into a ten-minute one.
Do extended statistics cost anything?
They add some time to ANALYZE and a little space in the catalog, and the mcv kind stores a list of common combinations. That is small. We should still create them only for column groups that appear together in real filters with real estimate problems, rather than for every pair of columns.
7Conclusion
In this answer, we saw that PostgreSQL multiplies selectivities as if columns were independent, so a filter on country and city, where city almost determines country, was estimated at 12 rows instead of 400,000, and the planner chose a nested loop that ran for minutes. Extended statistics with dependencies and mcv, followed by ANALYZE, gave a correct estimate and a hash join. We also saw raising the statistics target and rewriting the query as other levers, that hints come from an extension and are a last resort, and that MySQL 8 has histograms but no multi-column dependency statistics.
Open with
The planner estimates each condition separately from pg_stats and, by default, assumes the columns are independent, so it multiplies the selectivities. Country and city are not independent: Berlin almost always means Germany.
Then, if they want more
So the product is far too small, and the planner thinks the filter returns 12 rows when it returns 400,000. With 12 rows a nested loop with index lookups looks cheapest, but it actually runs 400,000 inner lookups, which is why it takes minutes.
I would confirm this in EXPLAIN ANALYZE by comparing estimated and actual rows on the addresses node. The fix in PostgreSQL is extended statistics: CREATE STATISTICS on country and city with dependencies and mcv, then ANALYZE, because the new statistics are only used after an analyze.
Then I would run EXPLAIN ANALYZE again and expect the estimate to be close to 400,000 and the join to become a hash join. Other levers are raising the statistics target for a skewed column or removing the redundant country condition.
I would avoid hints as a first fix, since PostgreSQL has none built in and pg_hint_plan freezes a plan as data changes. In MySQL 8, histograms help skewed columns, but there is no multi-column dependency statistic.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A query filters WHERE country = 'DE' AND city = 'Berlin'. The planner estimates 12 rows...": https://scrivio-interview-library.netlify.app/databases/indexing/6e40dbc726/