Learned Query Superoptimization

Ryan Marcus

Introduction

Database query optimization has traditionally been a "fire and forget" process. Optimizers are designed to find a "good enough" execution plan within milliseconds to ensure that the time spent optimizing does not exceed the time saved during execution. While this approach works for ad-hoc queries, it fails to account for the repetitive nature of modern analytical workloads. This paper proposes Learned Query Superoptimization, a new paradigm that treats query optimization as an offline, high-effort task for frequently executed query templates. By spending hours or even days optimizing a single template, we can achieve performance improvements that far outweigh the initial computational investment.

The Case for Superoptimization

The primary motivation for superoptimization is the massive mismatch between how optimizers are designed and how databases are actually used. In enterprise environments, such as those powering analytics dashboards, the same query templates are executed thousands or millions of times. An analysis of one year of query logs from a large corporate data warehouse revealed that nearly 11,000 query templates remained active for 6 to 12 months. The median template in this group was executed over 100,000 times. Furthermore, queries that lasted at least half a year accounted for 31% of the total cluster compute time.

Traditional optimizers are limited by narrow search spaces and inaccurate statistics. They often use heuristics to prune plans that seem "unpromising" (e.g., avoiding cross-joins) and rely on cardinality estimates that are notoriously prone to error. A superoptimizer, by contrast, can afford to explore unconventional plans and use empirical execution data rather than faulty cost models.

Methodologies for Superoptimization

The paper explores three primary directions for achieving superoptimization:

1. Latent Space Optimization

One of the most promising techniques involves encoding discrete query plans into a continuous, high-dimensional latent space. Using a neural Encoder-Decoder architecture, the discrete structure of a SQL plan is mapped to a vector space.

The Latent Space Optimization Pipeline: Encoding query trees into a continuous space for Bayesian Optimization and decoding them back into executable plans.

Once in this continuous space, researchers can apply Bayesian Optimization—a technique widely used in drug discovery—to "walk" through the space and identify optimal configurations. These points in the latent space are then decoded back into executable SQL plans. This allows the system to discover high-performance plans that traditional heuristic-based search would never consider.

2. Repeated Reinforcement Learning (RL)

While current RL-based optimizers attempt to balance exploration and exploitation, a superoptimizer tilts heavily toward exploration. It can autonomously experiment with diverse, "risky" plans on data samples. By using actual execution times to guide the learning process, the system avoids the pitfalls of inaccurate cost models and learns the optimal path through empirical evidence.

3. Program Synthesis for Bespoke Engines

Beyond just picking a better SQL plan, superoptimization can involve building a custom database engine for a single query. Using program synthesis, the system can generate bespoke data structures—such as specialized hashmaps and bitsets—and algorithms tailored to a specific high-frequency query.

Schema used for program synthesis experiments involving actors, movies, and production companies.

In a head-to-head test, a synthesized "bespoke" system using Cuckoo hashmaps and Roaring bitmaps was an order of magnitude faster than PostgreSQL for a join-heavy query. By breaking standard RDBMS rules (like strict adherence to Second Normal Form), these synthesized programs achieve performance levels that general-purpose systems cannot replicate.

Conclusion and Future Directions

Learned Query Superoptimization offers a path toward significant performance gains in data-intensive environments. However, several challenges remain. Identifying which queries deserve superoptimization is a non-trivial active learning problem. Additionally, while bespoke solutions are fast, they lack generality and must be re-synthesized if the schema changes. Integrating these intrusive techniques into production-grade DBMS without disrupting standard operations remains a significant engineering hurdle. Despite these challenges, the potential for order-of-magnitude speedups makes superoptimization a compelling frontier for database research.

Summary updated

BibTeX
@inproceedings{marcus2023learned,
  author = {Ryan Marcus},
  title = {{Learned Query Superoptimization}},
  booktitle = {Workshops @ VLDB},
  year = {2023},
}
Figure