Here is a summary of the paper "Revisiting Reuse in Main Memory Database Systems."
TL;DR
Traditional intermediate result reuse in databases relies on materializing temporary tables, which bottlenecks modern main-memory DBMSs at the memory bus. This paper introduces HashStash, a system that instead caches and reuses internal physical data structures—specifically hash tables—that are naturally generated during query execution. By avoiding extra materialization costs and leveraging a novel hardware-aware optimizer, HashStash achieves up to a 2x performance boost for analytical workloads without the overhead of traditional methods.
What Problem Does This Paper Address?
In analytical database workloads (like dashboards or interactive data exploration), consecutive queries often share overlapping computations. Historically, databases speed this up by saving the intermediate results of one query into a temporary table to be reused by the next.
However, this classic approach is fundamentally ill-suited for modern main-memory databases. Because data already lives in RAM, query execution is heavily optimized to keep tuples in ultra-fast CPU caches and registers. Forcing the database to write intermediate results out to a temporary table floods the memory bus and destroys cache locality. If the subsequent queries don't overlap enough to justify this upfront cost, traditional materialization can actually make the overall workload slower than just recomputing everything from scratch. The authors set out to find a way to get the benefits of reuse in main-memory systems without paying the crippling materialization penalty.
Methods
The authors propose HashStash, a system that reuses internal data structures rather than materialized result tables. Specifically, it targets hash tables built during hash-joins and hash-aggregations. Because these operators are "pipeline breakers" that must build hash tables in memory anyway, HashStash can cache them for free.
HashStash extends a classical DBMS architecture with two main components:
- Hash Table Manager (HTM): A cache that stores hash tables, tracks their lineage (how they were created), and manages memory using a coarse-grained Least Recently Used (LRU) garbage collection policy.
- Reuse-Aware Query Optimizer (RQO): A cost-based optimizer that decides whether reusing a cached hash table is cheaper than building a new one. It uses a highly calibrated cost model that accounts for CPU cache sizes, tuple widths, and the cost of inserting/looking up data.
Crucially, the optimizer supports four types of reuse:
- Exact: The cached table perfectly matches the query's needs.
- Subsuming: The cached table has more data than needed (requires post-filtering).
- Partial: The cached table is missing some data (requires inserting missing tuples).
- Overlapping: A mix of subsuming and partial (requires both inserting and filtering).
HashStash also supports multi-query reuse, extending the concept of "shared plans" to allow batches of concurrent queries to share these reuse-aware hash tables.
Datasets
- TPC-H (Scale Factor 10): The standard database benchmark schema and data were used for the main evaluation.
- Custom Interactive Workloads: The authors generated 64-query traces based on TPC-H to simulate interactive data exploration (zooming in/out, shifting, drill-down, roll-up). These were categorized into low (1%), medium (10%), and high (50%) reuse potential based on data overlap.
- Synthetic Micro-benchmarks: Used to calibrate the optimizer's cost models for hash table insertions and lookups across different CPU cache boundaries.
Key Insights and Ideas
- Free Materialization: By targeting pipeline breakers that must build a hash table to function, the system gets reusable artifacts without generating any additional memory bus traffic.
- Greedy Reuse is Dangerous: The authors found that an "always-share" heuristic (always reusing a hash table if it matches) can be significantly slower than recomputing. If a cached hash table is too large (causing CPU cache misses) or requires too much patching with missing tuples, it becomes a liability. HashStash's hardware-aware cost model successfully prevents these regressions.
- Flexibility is Key: Supporting partial and overlapping reuse massively increases the applicability of the system. Real-world users rarely run the exact same query twice; they tweak filters and groupings.
- Benefit-Oriented Optimization: The optimizer will sometimes intentionally choose a slightly more expensive initial query plan (e.g., rewriting an
AVGto aSUMandCOUNT, or building a hash table on a larger join input) if it predicts the resulting hash table will be highly reusable for future queries.
Key Results
- Speedup: On high-reuse workloads, HashStash achieved a 90% speedup (nearly 2x) over a baseline with no reuse.
- Beating Traditional Reuse: Traditional materialization-based reuse only achieved a 40% speedup on high-reuse workloads. Worse, on low-reuse workloads, traditional materialization slowed down execution by 10% due to overhead. HashStash had zero overhead in the low-reuse case, gracefully defaulting to standard execution.
- Multi-Query Batching: Using reuse-aware shared plans for batches of queries reduced total runtime by ~40% compared to executing them individually without reuse.
- Garbage Collection Efficiency: The coarse-grained LRU garbage collector introduced only a minimal ~10% runtime overhead, even when the system was constrained to just 20% of the memory required to store all hash tables.
Limitations and Future Work
- Limitations: The current HashStash prototype is single-threaded. While this was done intentionally to isolate the pure performance effects of the reuse models, it does not reflect the highly concurrent nature of production DBMSs. Additionally, it currently only supports hash tables.
- Future Work: The authors plan to extend this concept to other internal data structures, such as trees used for sorting operations. They also plan to explore concurrent reuse, where multiple active queries can share, read, and update the exact same hash table simultaneously.
Summary updated