Abstract
Window functions have been a part of the SQL standard since 2003 and are widely used in data analysis. However, as dataset sizes grow steeply, existing implementations are becoming inefficient. Recently, sampling-based algorithms like online aggregation have been proposed to offer a flexible trade-off between accuracy and efficiency, yet few have addressed window functions. In this paper, we extend our previous work and propose two new algorithms: the Range-based Global Sampling Algorithm (RGSA) and the Row-Labeled Sampling Algorithm (RLSA). These algorithms utilize global sampling rather than local sampling and demonstrate superior performance over baseline methods on the TPC-H benchmark dataset.
Introduction
Window functions (e.g., SUM(...) OVER (PARTITION BY... ORDER BY...)) are essential for modern data analysis, powering tasks like moving averages, ranking, and cumulative totals. Unlike standard aggregations that collapse rows, window functions produce a result for every row in the dataset, making them computationally "heavy." As datasets scale into the billions of rows, traditional sorting and partitioning methods struggle to meet the real-time requirements of online services.
The Computational Bottleneck
The standard implementation of a window function involves a sequence of expensive operations. As illustrated in Figure 1, the data must first be partitioned, then ordered within those partitions, and finally processed row-by-row within a defined sliding "frame." This process is inherently sequential and resource-intensive, leading to significant performance degradation as window sizes and table volumes increase.
Proposed Methodology: Global Sampling
Extending our previous work on Incremental Random Sampling (IRS), we introduce two "Global Sampling" approaches. Unlike previous attempts that tried to sample within each individual window (local sampling), our approach samples the entire table once. This avoids the significant overhead of repeated sampling operations for every row.
1. Range-based Global Sampling Algorithm (RGSA)
Designed for RANGE mode windows (e.g., "all rows within a 10-minute interval"), RGSA leverages the fact that value-based boundaries remain valid even after global sampling. This allows for efficient approximation without the need for complex re-indexing or mapping.
2. Row-Labeled Sampling Algorithm (RLSA)
ROW mode windows (e.g., "the 5 preceding rows") present a greater challenge because random sampling breaks the physical adjacency of rows. RLSA solves this by adding a subscript attribute to every sampled row. This mapping allows the algorithm to reconstruct the original row positions, effectively converting a ROW mode problem into a RANGE-style calculation based on original indices. This eliminates the "dirty data" problem where sampled rows are incorrectly associated with windows.
Statistical Confidence and Online Aggregation
To make these approximations actionable, we provide a statistical framework to calculate confidence intervals. As shown in Figure 2, the system provides an approximate aggregate (AGG) alongside a confidence level and an error interval. This transparency allows users to determine if the current approximation meets their precision requirements for decision support.
Evaluation and Results
We evaluated RGSA and RLSA using the TPC-H benchmark on tables with up to 15 million rows, including various data types (INTEGER, DOUBLE, VARCHAR, DATE) to ensure robustness. Key findings include:
- Performance: Our algorithms were significantly faster than PostgreSQL's native implementation. For a window size of 10,000 on a 1.5M row table, the sampling algorithms (at a 0.3 rate) completed in roughly one-third of the time required by the native implementation.
- Scalability: The execution time for RLSA and RGSA is remarkably insensitive to window size. While native execution time grows steeply as the window widens, the sampling approach remains relatively flat.
- Accuracy: Error rates were consistently low, often falling below 1% for a 0.5 sampling rate. In accordance with the Law of Large Numbers, the error rate drops significantly as the window size increases, making sampling particularly effective for the "long" windows that are typically the slowest to calculate.
Conclusion and Future Work
RGSA and RLSA provide a robust framework for speeding up SQL window functions through global sampling. By maintaining spatial relationships via subscript mapping and providing clear statistical guarantees, these algorithms enable high-performance analytics on massive datasets. Future work will focus on making the system more "intelligent," potentially allowing it to automatically determine the optimal sampling rate to meet a user's desired accuracy level without manual intervention.
Summary updated