Introduction
Query optimization is a fundamental component of database performance, responsible for determining the most efficient execution plan for SQL queries. Traditional optimizers rely heavily on hand-tuned heuristics and cost models that often fail to adapt to changing data distributions or learn from past execution mistakes. This "fire and forget" approach leads to brittle systems that require significant manual intervention from expert Database Administrators (DBAs). This paper proposes a vision for a "hands-free" query optimizer that leverages Deep Reinforcement Learning (DRL) to automatically tune itself and improve over time by learning from execution feedback.
The Challenge of DRL in Databases
While DRL has revolutionized fields like robotics and gaming, its application to query optimization faces unique hurdles. The search space for query plans is astronomically large, and the "reward" signal—query execution latency—is expensive to obtain. Waiting hours for a suboptimal plan to finish just to provide feedback to the model is impractical. Furthermore, standard cost models used by DBMSs are often inaccurate, making them unreliable proxies for true performance.
Proposed Architecture: ReJOIN
The authors introduce an architecture that integrates DRL into the query optimization pipeline. The system, exemplified by the prototype ReJOIN, transforms SQL queries into vector representations which serve as the initial state for a DRL agent.
To make DRL viable, the authors propose three core strategies:
- Learning from Demonstration (LfD): To avoid starting from a state of total ignorance, the DRL agent initially mimics the behavior of a traditional optimizer (e.g., PostgreSQL). This "imitation phase" ensures the agent learns reasonable baseline strategies without executing disastrously slow plans.
- Cost Model Bootstrapping: The agent first trains against existing cost models to learn basic structural properties of query plans. Once a baseline is established, it transitions to using real-world execution latency for fine-tuning.
- Incremental Learning: Complexity is managed by decomposing the problem. The agent starts with simple queries involving few relations and gradually tackles more complex joins. It also follows a staged pipeline, mastering join ordering before moving to index and operator selection.
Join Order Enumeration as a Markov Decision Process
In this framework, query optimization is modeled as a sequence of actions. The state represents the current set of joined and unjoined relations. An action consists of selecting two elements from the current state to join together.
As shown in the figure above, the agent starts with individual relations (A, B, C, D). By selecting actions (e.g., joining A and C), it transitions through intermediate states until a terminal state—a complete join tree—is reached. The final tree is then evaluated to provide a reward signal back to the agent.
Key Insights and Results
The ReJOIN case study yielded several significant findings:
- Efficiency: A trained DRL agent can generate plans faster than traditional greedy approaches. ReJOIN achieves a planning complexity of $O(n)$, compared to the $O(n^2)$ complexity of PostgreSQL's greedy optimizer.
- Performance: After processing approximately 9,000 queries, ReJOIN produced plans competitive with, and occasionally superior to, those generated by PostgreSQL in terms of both estimated cost and actual latency.
- Necessity of Specialized Strategies: Experiments showed that "naive" DRL—applying standard algorithms without LfD or bootstrapping—failed to outperform random selection even after 72 hours of training, highlighting the importance of the proposed methods.
Conclusion and Future Work
Transitioning to a DRL-based optimizer shifts the burden from manual heuristic tuning to managing "AI knobs" like neural architectures and learning rates. While this introduces new complexities, it paves the way for truly self-tuning database systems. Future work includes refining synthetic workload generation for better incremental learning and extending these techniques to other database management tasks like cache management and concurrency control.
Summary updated