Instance Optimal Join Size Estimation

Mahmoud Abo Khamis, Sungjin Im, Benjamin Moseley, Kirk Pruhs, Alireza Samadian

TL;DR

This paper introduces Welltris, the first "instance optimal" algorithm for estimating the size of relational joins that doesn't slow down when the join result is large. By leveraging geometric sampling techniques, the authors decouple estimation time from the number of rows in the join, while also providing a method to sample rows uniformly at random.

What Problem Does This Paper Address?

In database systems, estimating the size of an inner join (e.g., $T_1 \bowtie T_2 \bowtie T_3$) is a fundamental task for query optimizers. If you know a specific join will result in very few rows, you can prioritize it to avoid bloating intermediate results. However, exactly determining if a join is even non-empty is NP-hard.

To handle this, researchers use instance optimality. Instead of looking at the worst-case scenario for all possible inputs of a certain size, they look at how an algorithm performs on a specific instance compared to the best possible "proof" (or certificate) that the answer is correct for that instance.

Previous instance-optimal algorithms (like the "Tetris" algorithm) had a major flaw: their runtime was linear in the size of the join result ($z$). If the join produced millions of rows, the estimation process itself became prohibitively slow. The authors set out to create an algorithm that remains efficient even when the join size is massive.

Methods

The authors represent the join problem geometrically. Each row in a table is a point in $d$-dimensional space (where $d$ is the number of attributes). A "gap box" is a region in this space that contains no join results. A collection of these boxes serves as a "certificate" that the join size is no larger than the total volume minus the volume of the boxes.

Key components of their approach include:

  • Dyadic Gap Boxes: During preprocessing, tables are indexed into "dyadic" boxes (boxes whose dimensions are powers of two). This allows for efficient searching and storage using Trie-based data structures.
  • Welltris Algorithm: A variation of the earlier Tetris algorithm. While Tetris used exhaustive search to find points not covered by gap boxes, Welltris uses a randomized approach.
  • Klee’s Measure Problem: To avoid the linear dependency on the join size, Welltris incorporates a modified version of Chan’s algorithm. This geometric algorithm is typically used to find the volume of the union of many boxes. Welltris uses it to sample points uniformly at random from the "uncovered" space (the parts of the domain not yet proven to be empty).
  • Approximation via Sampling: By sampling $k$ points and checking how many fall into the actual join, the algorithm uses Chernoff bounds to provide a $(1+\epsilon)$ approximation of the total join size with high probability.

Datasets

This is a theoretical paper focused on algorithmic complexity and formal proofs. Consequently, no empirical datasets or benchmarks were used for evaluation. The "results" are mathematical proofs of runtime and approximation guarantees.

Key Insights and Ideas

  • Breaking the Output Dependency: The "aha moment" is realizing that you don't need to touch every row in a join to estimate its size. By sampling from the uncovered space (the space not yet identified as "empty" by gap boxes), the algorithm's complexity becomes a function of the "difficulty" of the instance (the certificate size $C$) rather than the output size $z$.
  • Geometric-Database Cross-pollination: The paper successfully applies sophisticated geometric algorithms (Chan’s algorithm for Klee’s measure) to a core database problem (join estimation), showing that the two fields share deep structural similarities.
  • Uniform Sampling as a Byproduct: The same mechanism used to estimate the size also allows the system to pull truly random rows from a join without materializing the entire join first. This is highly useful for machine learning applications that train on sampled data.

Key Results

  • Runtime: Welltris estimates the join size in $\tilde{O}(C^{d/2+1})$ time (ignoring poly-log factors and constants). Crucially, the variable $z$ (join size) is absent from this expression.
  • Accuracy: The algorithm provides a $(1+\epsilon)$ approximation with a probability of at least $1-\delta$.
  • Sampling Efficiency: The paper proves that $q$ points can be sampled uniformly at random in $\tilde{O}(C^{d/2+1} + qC)$ time.
  • Optimality: The authors argue that the exponential dependence on the number of dimensions $d$ is likely unavoidable, as the problem is $W[1]$-hard with respect to $d$.

Limitations and Future Work

  • The "Curse of Dimensionality": The runtime includes a $2^d$ or $C^{d/2}$ factor. While this is theoretically "optimal" given the hardness of the problem, it means the algorithm may struggle with joins involving a very high number of attributes.
  • Preprocessing Overhead: The algorithm relies on a preprocessing step to build the dyadic gap boxes. While this is done once per table, the cost and storage of these indexes in a real-world system aren't fully explored.
  • Lack of Empirical Testing: As a theoretical work, there is no data on how Welltris performs against existing industrial query optimizers (like those in PostgreSQL or SQL Server) on real-world workloads. Future work could involve implementing Welltris in a query engine to see if the theoretical gains translate to wall-clock speedups.

Summary updated

BibTeX
@inproceedings{khamis2021instance,
  author = {Mahmoud Abo Khamis and Sungjin Im and Benjamin Moseley and Kirk Pruhs and Alireza Samadian},
  title = {{Instance Optimal Join Size Estimation}},
  booktitle = {LAGOS},
  year = {2021},
  doi = {10.1016/j.procs.2021.11.019},
}
Figure