Introduction
Query optimization is a cornerstone of relational database management systems (RDBMS). Among the various tasks an optimizer performs, join ordering—determining the most efficient sequence in which to combine tables—is arguably the most critical and difficult. As the number of tables in a query grows, the search space of possible join orders expands exponentially, making the search for an optimal plan an NP-hard problem.
Traditional optimizers, such as those in PostgreSQL and SparkSQL, typically rely on fixed heuristics and cost models to navigate this complexity. However, these "one-size-fits-all" rules often fail to account for the non-linear costs associated with real-world hardware constraints, such as memory limits and disk I/O. This paper introduces DQ (Deep Q-optimizer), a novel approach that leverages Deep Reinforcement Learning (RL) to learn a tailored search strategy for join ordering.
Methodology: Join Ordering as a Sequential Decision Process
The core innovation of DQ is framing join ordering as a Markov Decision Process (MDP). In this framework, building a query plan is treated as a sequence of actions, where each action is a specific join between two relations.
Neural Network and Featurization
DQ utilizes a Multi-Layer Perceptron (MLP) to predict the long-term cumulative cost (the Q-value) of a join decision. To process SQL queries, the system "featurizes" the query graph. As shown in Figure 3, attributes are represented using one-hot encoding and scaled by their selectivity. This allows the neural network to understand the structure and data distribution of the query.
Data Efficiency and Training
A common hurdle for RL is the requirement for vast amounts of training data. DQ overcomes this by exploiting the Principle of Optimality: an optimal query plan is composed of optimal sub-plans. By executing a traditional optimizer once, DQ can extract thousands of training examples from the resulting sub-plans, significantly accelerating the learning process. Furthermore, DQ can be pre-trained on a standard cost model and subsequently fine-tuned using actual execution runtimes to correct for inaccuracies in the database's internal estimates.
Evaluation and Results
The researchers evaluated DQ using the Join Order Benchmark (JOB), based on the IMDB dataset, and the TPC-DS benchmark. These tests were designed to push the limits of join optimization in both real-world and synthetic scenarios.
Performance and Speed
In scenarios involving 15 or more tables, DQ demonstrated massive planning speedups—up to 10,000x faster than exhaustive search and 10x faster than standard left-deep heuristics. This speed is partially attributed to DQ's ability to "batch" join evaluations using SIMD/GPU instructions.
Robustness and Adaptability
DQ proved significantly more robust than traditional heuristics when faced with non-linear costs. For instance, when simulating memory pressure where data might spill to disk, DQ adapted its strategy to minimize sub-optimality. As illustrated in Figure 1, DQ consistently maintained lower sub-optimality compared to traditional left-deep search strategies across various workload types.
In the JOB workload, DQ achieved a 14% average speedup over the native PostgreSQL optimizer. In specific cases where cost models were inaccurate, fine-tuning DQ on real execution data resulted in plans up to 3.5x faster than those chosen by PostgreSQL.
Conclusion and Future Work
DQ demonstrates that Deep Reinforcement Learning can effectively master the complexities of join ordering, producing plans that are both faster to generate and more efficient to execute than those from traditional industrial optimizers. While challenges remain—such as handling complex literals in WHERE clauses and extending the model to outer joins—DQ represents a significant step toward the vision of fully "learned" database optimizers that adapt autonomously to their specific data and hardware environments.
Summary updated