Join Cardinality from Key Frequencies

Derive the output size of an equijoin using matching frequency products and identify why a hot key can dominate the result.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Count matches separately for each key
  2. Uniqueness or skew changes the estimate

Count matches separately for each key

If a key \(k\) occurs \(r_k\) times in the left input and \(s_k\) in the right, it contributes \(r_ks_k\) row pairs. The full output has size \(\sum_k r_ks_k\) over matching key values under bag semantics and the stated equality predicate. If frequencies are \((5,1)\) and \((4,2)\) on two keys, the output is \(5\cdot4+1\cdot2=22\). Knowing both tables have six rows does not determine that answer.

Uniqueness or skew changes the estimate

With at most one right row per key, each left row contributes at most one match. With many occurrences on both sides a single key can produce a quadratic-size result. Selectivity and correlation explain how estimators miss such patterns. Worst-case optimal joins address multiway queries where intermediate and final cardinalities can differ sharply.[1][2]

References

  1. CMU 15-445/645, Introduction to Database Systems: Lectures ↩
  2. PostgreSQL Global Development Group, PostgreSQL Documentation ↩