Algorithm Search⌘K

Theoremadvanced

Exponential propagation of estimation errors

Also called Ioannidis-Christodoulakis error propagation.

If each frequency that a join size estimate uses is off by at most a factor 1 + e, the estimate for a join of n relations is off by at most (1 + e)^n, and this bound is reached.

This theorem says that small errors in the statistics of single relations can become large errors in the estimated size of a join result, and that the growth is exponential in the number of relations in the worst case. Ioannidis and Christodoulakis proved results of this kind in 1991, in a model where each relation is described by the frequencies of its join attribute values. This page states and proves a clean form of the result in an explicit model, shows that the bound is reached, and separates it from the measurements of real systems. The estimation problem itself is on the cardinality estimation page.

Statement

Model. Let Q=R1⋈⋯⋈RnQ = R_1 \bowtie \cdots \bowtie R_n be a natural equality join of nn relations, with bag semantics. Call an attribute a join attribute if at least two of the relations have it, let AiA_i be the join attributes of RiR_i, and let XX be the set of all assignments of values to the join attributes. For yy an assignment to AiA_i, let fi(y)f_i(y) be the number of tuples of RiR_i whose values on AiA_i are yy. An estimator of the frequency kind has estimates f^i(y)≥0\hat f_i(y) \ge 0 of these frequencies and returns

S^=∑x∈X∏i=1nf^i(x∣Ai),\hat S = \sum_{x \in X} \prod_{i=1}^{n} \hat f_i(x|_{A_i}),

where x∣Aix|_{A_i} is the restriction of xx to AiA_i. Histograms with a uniform frequency in each bucket, frequencies from a sample, and the classic formulas built on the number of distinct values all give estimators of this kind. The q-error of an estimate S^\hat S of S>0S > 0 is max⁡(S^/S, S/S^)\max(\hat S/S,\ S/\hat S).

Theorem. Let S=∣Q∣S = |Q| and let c≥1c \ge 1.

  1. If every frequency estimate satisfies fi(y)/c≤f^i(y)≤c fi(y)f_i(y)/c \le \hat f_i(y) \le c\, f_i(y), then S/cn≤S^≤cnSS/c^n \le \hat S \le c^n S. So the q-error is at most cnc^n, for a join of nn relations, that is n−1n - 1 joins.
  2. The bound is reached: for every instance with S>0S > 0, the estimates f^i=c fi\hat f_i = c\, f_i satisfy the hypothesis of part 1 and give S^=cnS\hat S = c^n S, and f^i=fi/c\hat f_i = f_i / c gives S^=S/cn\hat S = S / c^n.

In plain words

To guess the size of a join, an optimizer multiplies guesses about each table. If each guess is a little too big, the product is too big by the small error times itself, once for each table. Ten tables that are each 10 percent too big give an answer that is about 2.6 times too big. Ten tables that are each twice too big give an answer that is a thousand times too big.

The theorem says that this is the worst that can happen when each table is off by a bounded factor, and that it really can happen. Real data adds a second problem: the guesses for different tables tend to be wrong in the same direction, so the errors do not cancel.

Proof

Lemma (the size of a join). S=∑x∈X∏i=1nfi(x∣Ai)S = \sum_{x \in X} \prod_{i=1}^{n} f_i(x|_{A_i}).

Proof. A result tuple of QQ is a choice of one tuple tit_i from each RiR_i such that any two chosen tuples agree on the attributes they share. Only join attributes are shared, so the chosen tuples agree exactly when there is one assignment x∈Xx \in X with ti∣Ai=x∣Ait_i|_{A_i} = x|_{A_i} for every ii, and this xx is unique, because every join attribute is in some AiA_i. For a fixed xx, the tuples tit_i can be chosen independently among the fi(x∣Ai)f_i(x|_{A_i}) tuples of RiR_i with these values, which gives ∏ifi(x∣Ai)\prod_i f_i(x|_{A_i}) result tuples. Summing over xx counts each result tuple once. □\square

Part 1. For each xx, every factor satisfies fi(x∣Ai)/c≤f^i(x∣Ai)≤c fi(x∣Ai)f_i(x|_{A_i})/c \le \hat f_i(x|_{A_i}) \le c\, f_i(x|_{A_i}), and all the numbers are at least 0, so the products satisfy

c−n∏ifi(x∣Ai)≤∏if^i(x∣Ai)≤cn∏ifi(x∣Ai).c^{-n} \prod_i f_i(x|_{A_i}) \le \prod_i \hat f_i(x|_{A_i}) \le c^{n} \prod_i f_i(x|_{A_i}).

Sum over xx and use the lemma: c−nS≤S^≤cnSc^{-n} S \le \hat S \le c^n S. When S>0S > 0, this gives S^/S≤cn\hat S/S \le c^n and S/S^≤cnS/\hat S \le c^n. □\square

Part 2. With f^i=c fi\hat f_i = c\, f_i, every product has the factor cnc^n, so S^=cnS\hat S = c^n S by the lemma, and the q-error is cnc^n. The case f^i=fi/c\hat f_i = f_i/c is the same with c−nc^{-n}. ■\blacksquare

Consequences

  • Numbers. With c=1.1c = 1.1 and n=10n = 10, the bound is 1.110≈2.591.1^{10} \approx 2.59. With c=1.5c = 1.5 it is about 57.7, and with c=2c = 2 it is 10241024. A plan whose intermediate result is a thousand times larger than estimated can be the wrong plan, for example a nested loop join where a hash join was needed.
  • Errors in the same direction (a consequence of the proof). If each relation is off by a factor cic_i in the same direction for all its frequencies, for example because a sample of the table was scaled wrongly, then S^/S=∏ici\hat S/S = \prod_i c_i exactly. In logarithms, log⁡(S^/S)=∑ilog⁡ci\log(\hat S/S) = \sum_i \log c_i. If the log⁡ci\log c_i are independent random variables with mean μ\mu and variance σ2\sigma^2, then log⁡(S^/S)\log(\hat S/S) has mean nμn\mu and variance nσ2n\sigma^2. So a systematic bias μ≠0\mu \ne 0 makes the error grow exponentially in nn, and errors with no bias still give a typical q-error of about eσne^{\sigma\sqrt n}, which also grows without limit.
  • Uniformity alone (an example in the model). Even exact table sizes and exact distinct counts give exponential errors when the data is skewed. Let R1,…,RnR_1, \ldots, R_n each have 100 tuples on one join attribute aa, with value uu in 99 tuples and value ww in 1 tuple. Then S=99n+1S = 99^n + 1. A uniform estimate uses f^i=100/2=50\hat f_i = 100/2 = 50 for both values and gives S^=2⋅50n\hat S = 2 \cdot 50^n. The ratio S/S^S/\hat S is about 1.96 for n=2n = 2, 15.2 for n=5n = 5 and 463 for n=10n = 10, close to 2n−12^{n-1}.
  • Measurements (not part of the theorem). Leis et al. (2015) measured the estimates of several systems on the Join Order Benchmark over the IMDB data. The estimates were mostly too small, and the errors grew quickly with the number of joins, which matches the second consequence with μ<0\mu < 0. Correlated columns, handled by the attribute value independence assumption, are a main reason for the common direction.
  • What an optimizer can do. Keep the error of each estimate small where it matters, as histograms built for joins try to do; use bounds instead of estimates, as pessimistic cardinality estimation does; or check the sizes at run time and re-plan, as mid-query re-optimization does. Moerkotte, Neumann and Steidl (2009) define the q-error and bound the extra cost of the chosen plan in terms of the q-errors of its estimates.

Tightness

Part 2 of the theorem shows that the factor cnc^n cannot be improved for any instance: the estimates that are off by exactly cc in the same direction for every frequency reach it. The exponent nn is the number of relations, so a chain of jj joins has exponent j+1j + 1. If the errors of the relations have different directions, they can cancel, and the bound is then far from the truth; the theorem gives only the worst case. If the hypothesis is weakened to an error of a factor cc in the total size of each relation, with no control of single frequencies, the q-error is not bounded at all: two relations of NN tuples each join in 0 tuples if their join values are disjoint and in N2N^2 tuples if all tuples have the same join value, while the sizes are the same.

History

Ioannidis and Christodoulakis (1991) studied the size of the results of equality joins in a model that describes each relation by the frequency distribution of its join attribute values, and showed that the error of the estimate can grow exponentially with the number of joins. In 1993 they studied which histograms limit this worst-case growth. Moerkotte, Neumann and Steidl (2009) introduced the q-error as the measure of these errors that fits multiplication. Leis et al. (2015) built the Join Order Benchmark and measured the growth in real systems.

References

  1. On the propagation of errors in the size of join results, the result that, in their model of equality joins described by the frequencies of the join attribute values, the error of the estimated result size grows exponentially with the number of joins
  2. Yannis E. Ioannidis and Stavros Christodoulakis, Optimal histograms for limiting worst-case error propagation in the size of join results, ACM Transactions on Database Systems 18(4) (1993), 709 to 748, histograms chosen to limit the worst-case error that propagates through joins
  3. Preventing bad plans by bounding the impact of cardinality estimation errors, the q-error, the larger of estimate/truth and truth/estimate, and bounds on the cost of the chosen plan in terms of it
  4. Viktor Leis, Andrey Gubichev, Atanas Mirchev, Peter Boncz, Alfons Kemper and Thomas Neumann, How good are query optimizers, really?, Proceedings of the VLDB Endowment 9(3) (2015), 204 to 215, Section 3 (measured cardinality estimates of several systems on the Join Order Benchmark, by number of joins)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/ZDG38A/text.yaml