Learned Offline Query Planning via Bayesian Optimization

Jeffrey Tao, Natalie Maus, Haydn Thomas Jones, Yimeng Zeng, Jacob R. Gardner, Ryan Marcus · DOI

Abstract

Traditional query optimizers are constrained by a strict "contract": they must produce an execution plan in milliseconds to avoid adding significant overhead to query latency. This constraint often forces the use of heuristics that miss globally optimal plans. This paper introduces BayesQO, a system that treats query optimization as an offline "superoptimization" problem. By mapping discrete query plans into a continuous latent space using Variational Auto-encoders (VAEs) and searching that space via Bayesian Optimization (BO), BayesQO identifies plans that are significantly faster than those produced by traditional or reinforcement learning (RL)-based optimizers. Our evaluation shows that for complex, repetitive analytics queries, BayesQO can achieve 10-100x performance improvements by focusing on best-arm identification rather than cumulative regret minimization.

1. Introduction

In modern data analytics, such as dashboarding and automated report generation, the same query structure is often executed thousands of times. In these scenarios, the traditional trade-off—where planning time must be a fraction of execution time—is no longer optimal. If a query runs for ten minutes daily, spending an hour of offline computation to find a plan that reduces execution to one minute yields a massive net gain.

Existing Learned Query Optimization (LQO) systems frequently utilize Reinforcement Learning (RL). However, RL is designed to minimize "regret"—the cumulative loss incurred during the learning process. In an offline superoptimization context, we do not care if the system tests 100 inefficient plans, provided the 101st plan is the fastest possible. BayesQO reframes this task as a black-box optimization problem, utilizing Bayesian Optimization to find the single best execution plan.

2. Methodology

BayesQO consists of three primary technical components designed to navigate the complex, discrete space of query plans.

2.1 Plan Encoding via VAE

Query plans are discrete, tree-like structures, making them difficult for standard optimization algorithms to navigate directly. BayesQO employs a Variational Auto-encoder (VAE) to map these structures into a 64-dimensional continuous "latent space." By training the VAE on millions of plans generated by traditional optimizers like PostgreSQL, the system ensures that the latent space is structured logically, where "nearby" points represent plans with similar performance characteristics.

2.2 Bayesian Optimization (BO)

Within the continuous latent space, BayesQO utilizes the TuRBO algorithm for Bayesian Optimization. The system builds a probabilistic surrogate model of query latency and uses Thompson Sampling as an acquisition function to determine which plan to evaluate next. This approach allows the optimizer to move smoothly through the space, balancing exploration of unknown regions with exploitation of known high-performance areas.

2.3 Handling Timeouts as Censored Data

A significant hurdle in query optimization is the "long tail" of bad plans, which can take hours to execute. BayesQO introduces a novel mechanism to handle these timeouts by treating them as right-censored observations. Rather than discarding a timed-out run, the model records that the latency is at least the timeout threshold. This mathematical constraint informs the surrogate model, allowing BayesQO to aggressively prune "bad neighborhoods" in the search space without waiting for every suboptimal plan to finish.

3. Experimental Evaluation

The authors evaluated BayesQO against four major benchmarks: the Join Order Benchmark (JOB), the Cardinality Estimation Benchmark (CEB), Stack (based on StackOverflow data), and the Data Services Benchmark (DSB). These benchmarks represent a range of challenges, from complex join correlations to large-scale tables.

3.1 Performance Results

BayesQO consistently outperformed state-of-the-art hint-based optimizers like Bao and RL-based systems like Balsa. On the JOB and Stack benchmarks, the system found plans that were not just marginally better, but orders of magnitude faster. For several complex queries, execution time was reduced from tens of seconds to mere milliseconds—a level of optimization unattainable by traditional optimizers operating within millisecond planning budgets.

3.2 Robustness and Data Drift

To test the longevity of learned plans, the researchers simulated two years of data growth. While some plans eventually regressed due to changing data distributions, BayesQO demonstrated high robustness. Furthermore, the system could "jump-start" new optimization runs using old plans as starting points, reaching a new optimum in approximately 1.5 hours, compared to the initial 8-hour exhaustive search.

4. Discussion and Limitations

While BayesQO represents a significant step forward in offline optimization, it currently focuses on join order and physical operator selection for PK-FK equijoins. It does not yet support subqueries or Common Table Expressions (CTEs) within the learned join tree. Additionally, the VAE requires GPU resources for efficient sampling, and the BO process adds a small overhead (single-digit seconds) per iteration.

Future work may involve integrating Large Language Models (LLMs) to generate better initialization points for the BO process, potentially bridging the gap between general learned patterns and the specific requirements of individual query instances.

5. Conclusion

BayesQO demonstrates that by shifting the query optimization paradigm from online regret minimization to offline best-arm identification, we can unlock massive performance gains for repetitive analytics. By combining VAE-based latent space encoding with Bayesian Optimization and a rigorous treatment of timeouts, BayesQO provides a robust framework for finding "super-optimized" plans that traditional systems simply cannot reach.

Summary updated

BibTeX
@article{tao2025learned,
  author = {Jeffrey Tao and Natalie Maus and Haydn Thomas Jones and Yimeng Zeng and Jacob R. Gardner and Ryan Marcus},
  title = {{Learned Offline Query Planning via Bayesian Optimization}},
  journal = {Proc. ACM Manag. Data},
  year = {2025},
  doi = {10.1145/3725316},
}
Figure