Deep Reinforcement Learning for Join Order Enumeration

Ryan Marcus, Olga Papaemmanouil

Abstract

Join order enumeration remains one of the most complex challenges in database query optimization. Traditional optimizers rely on static heuristics and cost models that do not adapt to specific workloads or learn from past performance. This paper presents ReJOIN, a novel framework that applies Deep Reinforcement Learning (DRL) to the join ordering problem. By modeling query planning as an iterative decision-making process, ReJOIN learns to generate join trees that are 20% more efficient than those produced by industry-standard optimizers like PostgreSQL, while also offering superior scalability for complex queries.

1. Introduction

In relational database management systems, the efficiency of a query is highly dependent on the order in which tables are joined. For a query involving $n$ relations, the number of possible join trees grows exponentially, making the search for an optimal plan a significant computational burden. Traditional optimizers typically use greedy heuristics or dynamic programming to navigate this search space. However, these methods are "forgetful"—they do not improve over time and often repeatedly select suboptimal plans for similar queries. ReJOIN addresses these limitations by introducing a learning-based approach that shifts the optimization burden from runtime search to an offline training phase.

2. The ReJOIN Framework

ReJOIN frames join order enumeration as a reinforcement learning problem where an agent (the optimizer) builds a join tree step-by-step by selecting pairs of relations to join.

2.1 Reinforcement Learning Architecture

The system utilizes a DRL loop consisting of an agent, an environment, and a reward mechanism. The agent employs Proximal Policy Optimization (PPO) to update a neural network with two hidden layers. This architecture allows the optimizer to develop a "memory" of effective strategies, enabling it to make better decisions as it encounters more queries.

The ReJOIN Reinforcement Learning architecture, illustrating the interaction between the environment, state layers, and action selection.

2.2 State Vectorization and Reward Signal

To process SQL queries, ReJOIN converts the database state into a vectorized format. This includes a tree structure encoding based on relation height, a join predicate matrix showing connectivity, and a selection predicate vector representing filters (e.g., Price > 100). The agent receives a reward upon completing a full join tree, defined as the reciprocal of the cost predicted by the database's internal cost model. This facilitates rapid training without waiting for actual query execution.

3. Experimental Evaluation

The authors evaluated ReJOIN using the Join Order Benchmark (JOB), based on the real-world IMDB dataset. The workload involved complex queries joining between 4 and 17 relations. The model was trained on 103 query instances and tested on 10 unseen queries to evaluate its ability to generalize to new data.

4. Key Results

Experimental results demonstrate that ReJOIN significantly outperforms the PostgreSQL baseline in both plan quality and optimization efficiency.

4.1 Plan Quality and Convergence

After training on approximately 10,000 queries, ReJOIN produced join orders that were, on average, 20% cheaper than those generated by PostgreSQL. As shown in the training progress, the agent's performance steadily improves, eventually surpassing the static baseline as it learns the nuances of the dataset's cost model.

Training progress of ReJOIN showing the reduction in query plan cost relative to PostgreSQL over 14,000 episodes.

4.2 Scalability and Efficiency

One of the most significant advantages of ReJOIN is its scalability. While traditional dynamic programming complexity grows exponentially with the number of tables, ReJOIN’s planning time scales linearly. At runtime, the neural network simply performs fast matrix multiplications (inference) to decide the next join, making it significantly faster than traditional optimizers for queries with 10 or more relations.

5. Discussion and Future Work

While ReJOIN proves the viability of DRL for query optimization, it currently depends on the accuracy of the database's internal cost model. If the cost model provides poor cardinality estimates, the agent will efficiently learn a suboptimal strategy. Future work could involve using actual execution latency as a reward signal. Additionally, "bootstrapping" the model with existing heuristic knowledge could mitigate the "cold start" problem associated with training from scratch.

6. Conclusion

ReJOIN represents a paradigm shift in query optimization, moving from static algorithms to dynamic, learning-based systems. By providing better plans and faster optimization for complex queries, it demonstrates the potential for Deep Reinforcement Learning to solve long-standing challenges in database systems.

Summary updated

BibTeX
@inproceedings{marcus2018deep,
  author = {Ryan Marcus and Olga Papaemmanouil},
  title = {{Deep Reinforcement Learning for Join Order Enumeration}},
  booktitle = {Aidm@sigmod},
  year = {2018},
  doi = {10.1145/3211954.3211957},
}
Figure