Reservoir Sampling over Joins

Binyang Dai, Xiao Hu, Ke Yi

This paper, "Reservoir Sampling over Joins," was presented at SIGMOD 2024. It tackles a classic but notoriously difficult problem in database systems: how to maintain a random sample of a join's output when the input data is a high-speed stream.

TL;DR

The authors present the first near-linear time algorithm for maintaining uniform random samples over join results in a streaming environment. By combining a novel "reservoir sampling with predicate" technique with a specialized dynamic index, they achieve up to a 147x speedup over previous state-of-the-art methods, making real-time analytics on massive joins computationally feasible.

What Problem Does This Paper Address?

In large-scale data analytics, we often need to join multiple tables to answer queries or train machine learning models. However, join operations are "explosive"—the number of results can be polynomially larger than the input data. Materializing the full join is often too slow and memory-intensive.

Sampling is the standard solution: if we can get a representative 1% sample of the join, we can estimate the total result with high confidence. The challenge arises in the streaming setting, where new data arrives continuously. To maintain a "reservoir" (a fixed-size uniform sample) of the join results, a naive approach would be to re-run the sampling process every time a new tuple arrives. This results in $O(N^2)$ complexity, which is a death sentence for high-velocity streams.

The core technical hurdle is that the sampling operator cannot be "pushed down" below the join. Mathematically, the sample of a join is not equal to the join of the samples. Therefore, you must find a way to sample the output space without ever actually building the full output.

Methods

The authors' solution rests on two primary technical pillars:

  1. Reservoir Sampling with a Predicate: Traditional reservoir sampling (like Vitter’s algorithm) uses a "skip" operation to jump over items that won't enter the sample, which is very efficient. The authors generalize this to handle a predicate. This allows the sampler to treat certain potential join results as "dummies" (items that fail the predicate). They prove that as long as the stream of results is "dense" (meaning a constant fraction of items are real), the algorithm remains highly efficient.
  2. A Dynamic Index for Sampling: To make the "skip" operation work, you need an index that can tell you, "Give me the $i$-th result of the join," very quickly. The authors designed a dynamic index for acyclic joins that supports updates in $O(\log N)$ time. Crucially, this index doesn't just track real join results; it tracks a superset that includes "dummy" results. This "approximation" is what allows the index to be updated so much faster than previous versions that tried to be perfectly precise.

By combining these, when a new tuple arrives, the system treats the new potential join results as a "batch." The sampler uses the index to "jump" through this batch, only inspecting a few items to update the reservoir.

Datasets

The authors evaluated their approach (dubbed RSJoin) using both graph and relational data:

  • Epinions (Graph): A social network dataset from SNAP used to test line-joins, star-joins, and dumbbell joins.
  • TPC-DS: The industry-standard benchmark for decision support systems, featuring complex relational schemas and foreign-key constraints.
  • LDBC Social Network Benchmark (SNB): A benchmark focused on join-heavy complex queries in a social network context.

Key Insights and Ideas

  • The "Density" Trade-off: The most brilliant insight is the move from precision to "density." Previous researchers struggled to build indexes that only contained real join results. This paper argues that it is better to have an index that is "mostly" correct (dense) and easy to update, then let a predicate-aware sampler filter out the "dummies."
  • Instance Optimality: The authors don't just provide a "fast" algorithm; they prove that their reservoir sampling with a predicate is instance-optimal. This means no other algorithm can perform significantly better on a given distribution of real vs. dummy items.
  • Handling Cyclic Joins: While the core focus is acyclic joins, the authors show the method extends to cyclic joins (like triangles in a graph) using Generalized Hypertree Decomposition (GHD), though the complexity then scales with the "width" of the query.

Key Results

  • Massive Speedups: Compared to the previous state-of-the-art (SJoin), RSJoin was between 4.6x and 147.6x faster across various queries.
  • Update Efficiency: On a line-4 join, the average update time per tuple was just 13 microseconds, whereas previous methods lacked any theoretical guarantee on update time and often stalled on large datasets.
  • Scalability: RSJoin successfully processed joins that produced over 3.7 billion results while maintaining a fixed-size sample, whereas competing methods failed to finish within a 12-hour limit.
  • Near-Linear Complexity: The total running time is $O(N \log N + k \log N \log \frac{N}{k})$, where $N$ is the stream length and $k$ is the sample size. This is a massive theoretical leap from the $O(N^2)$ worst-case of prior work.

Limitations and Future Work

  • Insertion-Only: The current work focuses on streams where data is only added. Handling deletions (a "fully dynamic" stream) is significantly harder; the authors note that maintaining a sample under deletions for certain queries likely requires at least $\Omega(N^{1.5})$ time.
  • Projections: The paper focuses on join queries. Adding a projection operator (e.g., SELECT DISTINCT) introduces new challenges because multiple join results might project to the same output tuple, complicating the "uniformity" of the sample. The authors suggest this as a promising direction for future research.

Summary updated

BibTeX
@article{dai2026reservoir,
  author = {Binyang Dai and Xiao Hu and Ke Yi},
  title = {{Reservoir Sampling over Joins}},
  journal = {ACM Trans. Database Syst.},
  year = {2026},
  doi = {10.1145/3787855},
}
Figure