Abstract
Join optimization is one of the most important research problems in database systems. Traditional join optimizers are usually proposed based on heuristics, which are expensive and often fail to generate the optimal execution plan. There are two reasons accounting for this. (1) The optimizers are based on heuristics and only explore a subset of the search space. (2) They do not use the history logs and cannot estimate the goodness of their generated plans on a specific join problem. To tackle these challenges, we propose RLO, a reinforcement learning-based optimizer for join optimization. We model the join optimization problem as a Markov decision process and use deep $Q$-learning to estimate the possible reward of a possible operation. To boost the effectiveness of RLO, we further propose a tree-based embedding method to represent the “state and use a beam search to avoid missing the optimal plans. We implement RLO in Apache Calcite and Postgres. Extensive experiments demonstrate that: (1) Apache Calcite RLO is $10~\times$–$56~\times$ faster in finding the execution plan and 80% faster in executing the plan than the state-of-the-art heuristics. (2) Compared with the native Postgres implementation, RLO can be $14~\times$ faster in finding the execution plan and 12.9% faster in an end-to-end comparison.
Introduction
In relational database systems, the join operation is fundamental yet computationally expensive. Join optimization—the process of determining the most efficient order to join multiple tables—is an NP-hard problem. As the number of tables in a query increases, the search space of possible join orders grows exponentially.
Traditional optimizers typically rely on fixed heuristics (e.g., left-deep trees) or static cost models. However, these methods have significant drawbacks: they often prune the search space too aggressively, missing optimal "bushy" plans, and their cost models frequently fail to reflect real-world data distributions or learn from past execution errors.
The RLO Framework
RLO (Reinforcement Learning Optimization) addresses these limitations by treating join optimization as a learning task. By modeling the problem as a Markov Decision Process (MDP), RLO can leverage historical execution data to improve its decision-making over time.
Markov Decision Process Formulation
In the RLO framework, the optimization process is broken down into a sequence of steps:
- State: The current partial join tree and the set of remaining relations.
- Action: Selecting two relations (or sub-trees) to join next.
- Reward: A value derived from the estimated cost of the resulting plan, which the agent seeks to maximize (or minimize cost).
Deep Q-Learning and Tree-based Embedding
RLO utilizes a Deep Q-Network (DQN) to predict the long-term value (Q-value) of specific join actions. A critical innovation in RLO is the tree-based embedding method. Unlike traditional one-hot encodings that ignore the structure of the join tree, RLO's embedding captures the hierarchical position of each table. By encoding a table's depth as $1/2^h$, the neural network gains "structural awareness," allowing it to better evaluate complex bushy join structures.
Advanced Search and Fine-Tuning
To ensure high-quality plans, RLO incorporates two additional mechanisms:
- Beam Search: Instead of a purely greedy approach that might lead to local optima, RLO maintains a "beam" of the most promising candidate plans. This allows the optimizer to explore multiple paths simultaneously, increasing the likelihood of finding the global optimum.
- Execution-Based Fine-Tuning: RLO can bridge the gap between theoretical cost models and actual performance. By feeding real execution times back into the model, the system "learns" the true characteristics of the underlying hardware and data distribution.
Experimental Evaluation
The researchers evaluated RLO using the Join Order Benchmark (JOB) on the IMDB dataset, which is known for its challenging data skew.
- Apache Calcite Integration: RLO outperformed traditional heuristics significantly, finding plans 10x to 56x faster. More importantly, the resulting plans were 80% faster to execute.
- PostgreSQL Integration: Even when compared against the highly mature Postgres optimizer, RLO demonstrated a 14x improvement in plan generation speed and a 12.9% improvement in total end-to-end query execution time.
Conclusion
RLO represents a significant step forward in database optimization. By replacing rigid heuristics with a structurally aware reinforcement learning model, it achieves faster optimization times and superior execution plans. While challenges remain regarding training overhead and generalization across different schemas, RLO proves that "teaching" a database to optimize itself is a viable and highly effective strategy for modern data management.
Summary updated