A Sampling Algebra for Aggregate Estimation

Supriya Nirkhiwale, Alin Dobra, Chris Jermaine

Abstract

Modern database systems increasingly rely on sampling to provide fast, approximate answers to aggregate queries over massive datasets. However, providing rigorous confidence intervals for these estimates remains a challenge, particularly for complex queries involving multiple joins. This paper introduces a "sampling algebra" based on Generalized Uniform Sampling (GUS) and a novel concept of Second Order Analytical (SOA) equivalence. By allowing sampling operators to commute with standard relational operators, this framework enables the automatic calculation of expected values and variances for arbitrary query plans, facilitating accurate error bounds without requiring specialized database engines.

Introduction

As data volumes explode, the TABLESAMPLE clause in SQL has become a vital tool for interactive data analysis. While sampling significantly reduces query latency, it introduces uncertainty. For simple queries on a single table, calculating confidence intervals is straightforward. However, real-world analytical queries often involve complex joins. Joining sampled tables creates intricate statistical correlations; for instance, if a tuple from a primary table is excluded from a sample, all corresponding joined tuples in the result set are also lost.

Existing methods for estimating error in joined samples are often restricted to specific schemas (like star schemas) or require non-standard execution engines. There is a clear need for a unified theoretical framework that can analyze arbitrary sampling methods within complex query plans and provide reliable error estimates.

The GUS Framework and SOA Equivalence

The authors propose a generalized approach to sampling called Generalized Uniform Sampling (GUS). Rather than treating Bernoulli sampling or sampling without replacement (WOR) as distinct cases, GUS abstracts them into a "quasi-operator" that acts as a randomized filter. A GUS method is defined by the inclusion probabilities of individual tuples and pairs of tuples, determined by their lineage.

Mapping standard sampling methods to GUS parameters.

To make this framework actionable, the paper introduces Second Order Analytical (SOA) Equivalence. Traditional relational algebra requires two plans to be identical in their output sets. SOA-equivalence relaxes this requirement: two query plans are considered equivalent if they yield the same first and second statistical moments—specifically, the same expected value and variance—for any linear aggregate like SUM.

A Commutative Sampling Algebra

The core contribution of this work is the development of a Sampling Algebra. Using the principle of SOA-equivalence, the authors prove that GUS operators can commute with standard relational operators such as Selections, Joins, and Unions.

This commutativity is transformative for query analysis. It allows an analytical tool to take a complex execution plan where sampling is scattered throughout and mathematically "push" these sampling operators to the top of the plan, immediately preceding the final aggregate. Once the sampling is consolidated at the top, standard statistical formulas (e.g., Chebyshev bounds or normal approximations) can be applied to derive confidence intervals.

Transformation of query plans through the commutativity of GUS operators.

The authors demonstrate that these GUS operators form a mathematical semi-ring under Union and Compaction (intersection). This property allows for the composition of complex, multi-dimensional sampling strategies from simpler building blocks.

Implementation and Practical Results

The primary results are theoretical proofs showing that GUS operators commute with Selections (Proposition 5) and Joins (Proposition 6) under SOA-equivalence. Crucially, the join commutativity proof requires that joined tables have disjoint lineage, which precludes self-joins but covers the vast majority of analytical use cases.

For practical implementation, the authors suggest an external "SBox" module. This module can rewrite standard SQL queries to extract tuple lineage and compute variance coefficients on the fly. To mitigate the computational overhead of variance estimation, the paper proves that a second layer of sub-sampling can be used to estimate the variance itself efficiently without compromising statistical validity.

Limitations and Future Directions

The current framework is optimized for linear aggregates such as SUM and COUNT. While AVERAGE can be handled via the delta method, non-linear aggregates like MIN, MAX, and DISTINCT remain outside the scope of this algebra due to their inherent non-linearity. Additionally, the restriction against self-joins remains a theoretical boundary.

Future work may extend this framework to handle DISTINCT queries by exploring connections to "safe plans" in probabilistic databases. Other potential applications include load shedding in data streams and improving intermediate relation size estimation during query optimization.

Summary updated

BibTeX
@article{nirkhiwale2013sampling,
  author = {Supriya Nirkhiwale and Alin Dobra and Chris Jermaine},
  title = {{A Sampling Algebra for Aggregate Estimation}},
  journal = {Proc. VLDB Endow.},
  year = {2013},
  doi = {10.14778/2556549.2556563},
}
Figure