Cost-based Query Rewriting Techniques for Optimizing Aggregates Over Correlated Windows

Wentao Wu, Philip A. Bernstein, Alex Raizman, Christina PavlopoulouarXiv 2020

Abstract

Window aggregates are ubiquitous in stream processing. In Azure Stream Analytics (ASA), many customer queries contain aggregate functions over multiple correlated windows defined on the same event stream. We present a cost-based optimization framework for optimizing such queries by sharing computation among multiple windows. We introduce "factor windows"—auxiliary windows that help reduce overall computation cost. Our optimizer produces rewritten query plans with lower costs than the original plans. These techniques are engine-agnostic and can be implemented on any stream processing system supporting SQL-like languages. Evaluation shows throughput improvements of up to 16.8x.

Introduction

In modern stream processing environments like Azure Stream Analytics, users frequently monitor metrics across various time scales simultaneously. For instance, a dashboard might display the minimum temperature over the last 20, 30, and 40 minutes. A naive execution engine would compute each window independently, leading to significant redundant computation as these windows overlap substantially.

While window slicing techniques exist, they often fail to exploit the specific overlapping relationships between correlated windows. Furthermore, existing stream optimizers are predominantly rule-based, missing opportunities for cost-based efficiencies. This paper addresses these gaps by proposing a framework that rewrites query plans to maximize shared computation.

Methodology

The proposed optimization framework follows a structured approach to transform inefficient query plans into high-performance execution graphs.

Window Coverage Graph (WCG) and Cost-based Optimization

The relationship between windows is formalized using a Directed Acyclic Graph (DAG) called a Window Coverage Graph (WCG). An edge from window $W_i$ to $W_j$ indicates that the results of $W_i$ can be used to compute $W_j$. The optimizer's goal is to find a "min-cost WCG" that minimizes the total CPU overhead based on a cost model that estimates processing time.

Figure 6: Transition from an initial Window Coverage Graph to a min-cost version by selecting lower-cost computation paths.

Factor Windows

A key innovation is the introduction of "factor windows"—auxiliary windows not present in the original query. By strategically inserting these hidden windows, the system creates a "common denominator" for computation. For example, a 10-minute factor window can serve as a building block for both 20-minute and 30-minute windows, reducing the total volume of raw events processed. Finding the absolute optimal set of factor windows is a variation of the NP-hard Steiner Tree problem; the authors provide efficient heuristic algorithms that yield near-optimal results in milliseconds.

Figure 1: Comparison of the original query plan, a basic rewritten plan, and a plan optimized with factor windows.

Engine-Agnostic Implementation

Because the optimization occurs at the query plan level, it is engine-agnostic. The framework rewrites declarative SQL-like queries into optimized expressions for engines like Trill or Flink without requiring modifications to the underlying execution core.

Evaluation

The framework was evaluated using synthetic datasets (1M and 10M events) and a real-world dataset (Real-32M) from the DEBS 2012 Grand Challenge.

Key Results

  • Massive Throughput Gains: Rewritten plans achieved up to 16.8x higher throughput on synthetic data and 9.1x on real-world data compared to original plans.
  • Efficiency vs. State-of-the-Art: The method outperformed or matched state-of-the-art window slicing frameworks like Scotty while being easier to integrate into existing systems.
  • Low Overhead: The optimization process is highly efficient, typically completing in under 100 milliseconds even for complex queries involving over 20 windows.

Conclusion

This cost-based rewriting framework significantly enhances the performance of correlated window aggregates in stream processing. By utilizing factor windows and a robust cost model, it achieves massive throughput improvements with minimal optimization overhead. Future work includes extending support to holistic aggregate functions like MEDIAN and adapting to dynamic, bursty traffic patterns.

Summary updated

BibTeX
@misc{wu2020cost,
  author = {Wentao Wu and Philip A. Bernstein and Alex Raizman and Christina Pavlopoulou},
  title = {{Cost-based Query Rewriting Techniques for Optimizing Aggregates Over Correlated Windows}},
  year = {2020},
  eprint = {2008.12379},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2008.12379},
}
Figure