Avoiding Materialisation for Guarded Aggregate Queries

Matthias Lanzinger, Reinhard Pichler, Alexander Selzer

Introduction

Analytical queries in modern database management systems (DBMSs) often face a significant performance hurdle known as the "intermediate result explosion." This occurs when joining multiple large tables to produce a small set of aggregate values, such as a SUM or MEDIAN. Even if the final output is just a few numbers, the joins required to reach them can generate billions of intermediate rows, consuming vast amounts of memory and processing time.

This paper introduces a novel optimization framework designed to eliminate this bottleneck for a broad class of practical SQL queries. By shifting the focus from materializing full tuples to propagating frequencies and partial aggregates up a join tree, the proposed approach achieves significant speedups and memory efficiency.

The Guarded Query Framework

The core of the optimization lies in the definition of guarded aggregate queries. A query is considered "guarded" if a single relation (the "guard") contains all the attributes used in the GROUP BY clause and all aggregate expressions. This concept is further extended to piecewise-guarded queries, allowing different aggregates within the same query to have different guards.

Comparison of standard query execution versus the proposed frequency-based optimization.

As shown in the query plan comparison above, instead of performing standard joins that duplicate data, the system performs semi-joins and propagates a "frequency" attribute (effectively a COUNT(*)) up the join tree. This allows the engine to account for the multiplicity of tuples without actually creating redundant intermediate rows.

Frequency Propagation and AggJoin

The transition from logical optimization to physical execution is handled by a new operator called AggJoin. Unlike standard join operators, AggJoin is "aggregation-aware." It maintains running counts (frequencies) and partial aggregate values (such as running sums or min/max bounds) during the join process itself.

Conceptual view of frequency propagation within a join tree structure.

This method relies on the insight that for many aggregate functions, the actual joined rows are unnecessary; only the multiplicity of the values matters. Propagating a single integer frequency is exponentially more efficient than materializing millions of duplicate rows. This approach ensures that the size of the data moved through the query plan is linearly bounded by the size of the input data, effectively preventing the intermediate explosion entirely.

Performance and Scalability

The authors evaluated their implementation within Spark SQL across several industry-standard benchmarks, including TPC-H, TPC-DS, and the Join Order Benchmark (JOB). The results demonstrate massive improvements in both speed and resource consumption:

  • Speedups: On the STATS-CEB benchmark, the optimized system was 24x faster than standard Spark SQL. For complex graph queries, speedups exceeded 100x.
  • Memory Efficiency: The peak number of materialized tuples was reduced by 3 to 5 orders of magnitude. Queries that previously required $10^{10}$ intermediate tuples were executed using only $10^5$.
  • Robustness: The system successfully handled complex path queries on large SNAP datasets that caused standard Spark SQL to crash due to "Out of Memory" errors or time out after 30 minutes.

Crucially, the AggJoin operator introduces zero overhead for simple queries where intermediate results are not a bottleneck, making it a safe default optimization for modern analytical engines.

Conclusion and Future Work

While the current framework provides a powerful solution for acyclic guarded queries, some limitations remain. The theory currently relies on queries being acyclic, though cyclic queries can be transformed using hypertree decompositions. Additionally, while the strict "guarded" class supports all ANSI SQL aggregates, the more flexible "piecewise-guarded" class is currently limited to functions like SUM, COUNT, and AVG.

Future research will explore techniques to "force" guardedness by performing small upfront joins, potentially expanding these zero-materialization benefits to an even wider array of complex, unguarded analytical workloads.

Summary updated

BibTeX
@inproceedings{lanzinger2024avoiding,
  author = {Matthias Lanzinger and Reinhard Pichler and Alexander Selzer},
  title = {{Avoiding Materialisation for Guarded Aggregate Queries}},
  booktitle = {AMW},
  year = {2024},
}
Figure