Reservoir Sampling over Joins

Binyang Dai, Xiao Hu, Ke Yi

Abstract

Maintaining uniform random samples over join results in a streaming environment is a foundational challenge for real-time database analytics. Traditional methods often suffer from quadratic complexity or lack theoretical guarantees on update times. This paper, presented at SIGMOD 2024, introduces RSJoin, the first near-linear time algorithm for this problem. By integrating a novel reservoir sampling technique that accounts for predicates with a specialized dynamic index, the authors achieve significant performance gains, including up to a 147x speedup over existing state-of-the-art methods.

1. Introduction

In modern large-scale data analytics, join operations are essential for synthesizing information across multiple tables. However, joins are inherently "explosive"; the cardinality of a join result can be polynomially larger than the input streams. Materializing these results in full is often computationally prohibitive and memory-intensive, particularly in high-velocity streaming contexts.

Sampling offers a viable alternative, allowing systems to estimate query results or train machine learning models using a representative fraction of the data. The primary hurdle in the streaming setting is that the sampling operator cannot be "pushed down" below the join—mathematically, a sample of a join is not equivalent to a join of samples. Consequently, a system must sample from the output space without ever materializing the full output. Naive approaches that re-run sampling upon every new tuple arrival result in $O(N^2)$ complexity, which is unsustainable for real-time applications.

2. Methodology

The authors propose RSJoin, which rests on two primary technical innovations designed to bypass the $O(N^2)$ bottleneck.

2.1 Reservoir Sampling with a Predicate

Standard reservoir sampling algorithms, such as Vitter’s algorithm, utilize a "skip" operation to efficiently bypass items that will not enter the sample. RSJoin generalizes this to handle a predicate. This allows the sampler to treat potential join results that do not meet specific criteria as "dummies." The authors prove that as long as the stream of results remains "dense"—meaning a constant fraction of the items are valid—the algorithm maintains high efficiency. Furthermore, they demonstrate that this predicate-aware sampler is instance-optimal, meaning no other algorithm can significantly outperform it for a given distribution of real versus dummy items.

2.2 Dynamic Indexing for Sampling

To facilitate the "skip" operation, the system requires an index capable of rapidly retrieving the $i$-th result of a join. RSJoin utilizes a dynamic index designed for acyclic joins that supports updates in $O(\log N)$ time.

A key insight of this work is the trade-off between precision and density. Rather than building an index that tracks only valid join results (which is computationally expensive to maintain), the authors track a superset that includes "dummy" results. This approximation allows for much faster index updates. The sampler then filters these dummies, relying on the "density" of the results to ensure that the number of wasted operations remains low.

2.3 Extension to Cyclic Joins

While the core algorithm focuses on acyclic joins, the authors extend the methodology to cyclic joins (e.g., triangle queries in graphs) using Generalized Hypertree Decomposition (GHD). In these cases, the complexity scales according to the "width" of the query, providing a flexible framework for complex relational structures.

3. Experimental Evaluation

The authors evaluated RSJoin against the previous state-of-the-art (SJoin) using three diverse datasets:

  • Epinions (Graph): Used to test various join topologies, including line, star, and dumbbell joins.
  • TPC-DS: An industry-standard benchmark for decision support, featuring complex relational schemas.
  • LDBC Social Network Benchmark (SNB): Focused on join-heavy queries within a social network context.

3.1 Performance Results

  • Throughput and Speed: RSJoin demonstrated speedups between 4.6x and 147.6x compared to SJoin.
  • Update Latency: For a line-4 join, the average update time per tuple was approximately 13 microseconds. Previous methods lacked theoretical guarantees on update times and frequently stalled on larger datasets.
  • Scalability: RSJoin successfully processed joins producing over 3.7 billion results while maintaining a fixed-size reservoir. Competing methods failed to complete these tasks within a 12-hour window.
  • Complexity: The algorithm achieves a total running time of $O(N \log N + k \log N \log \frac{N}{k})$, where $N$ is the stream length and $k$ is the sample size. This represents a significant theoretical improvement over the $O(N^2)$ worst-case complexity of prior work.

4. Conclusion and Future Work

RSJoin provides a robust, near-linear time solution for maintaining uniform samples over streaming joins. By prioritizing "dense" indexing over perfect precision, the authors have made real-time analytics on massive join results computationally feasible.

However, certain challenges remain. The current implementation is insertion-only. Handling deletions in a "fully dynamic" stream is significantly more complex; the authors note that maintaining samples under deletions for specific queries likely requires at least $\Omega(N^{1.5})$ time. Additionally, incorporating projection operators (e.g., SELECT DISTINCT) remains a future research direction, as multiple join results may project to the same output tuple, complicating the maintenance of a uniform sample.

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