NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Learning State Representations for Query Optimization with Deep Reinforcement Learning

Sathiya Keerthi Selvaraj, Jennifer Ortiz, Magdalena Balazinska, Johannes Gehrke, S. Sathiya KeerthiDeem@sigmod 2018

TL;DR

This paper proposes a novel approach to query optimization by using deep reinforcement learning to learn state representations of subqueries. By encoding database and query properties into learned latent vectors, the system aims to improve cardinality estimation and ultimately generate more optimal query execution plans than traditional methods.

What Problem Does This Paper Address?

Query optimization in database management systems (DBMSs) is a notoriously difficult problem. Existing systems often rely on simplifying assumptions about data distributions (like uniformity) for cardinality estimation, which can lead to significant errors when data is correlated or non-uniform. This, in turn, results in suboptimal query execution plans. While advanced techniques like multidimensional histograms and sampling exist, their overhead often prevents their widespread adoption. The authors aim to leverage the power of deep learning to create more accurate cardinality estimates and, consequently, better query plans by learning representations of intermediate query results.

Methods

The core of the paper's contribution lies in its approach to learning state representations for subqueries. Instead of directly feeding raw query and database features into a model, they propose a recursive method:

  • State Representation Learning: A neural network, NNST (Neural Network State Transition), is trained to generate a latent vector representation ($h_t$) for each subquery at a given state ($t$). This representation is learned by taking the previous subquery's representation ($h_{t-1}$) and the current operation (action, $a_{t-1}$) as input.
  • State Transition Function: NNST acts as the state transition function, mapping $(h_{t-1}, a_{t-1})$ to $h_t$. This learned representation aims to capture essential properties of the subquery's intermediate results.
  • Cardinality Prediction: Another network, NNObserved, is trained to predict observed variables (like cardinality) from these learned state representations. The training of NNST and NNObserved is done jointly, with the goal of learning representations that are informative for cardinality estimation.
  • Initialization (NNinit): For the initial state, a separate network, NNinit, is used. It takes database properties ($x_0$) and the first operation ($a_0$) to predict the cardinality of the initial subquery.
  • Reinforcement Learning for Plan Enumeration: The learned state representations are then intended to be used within a reinforcement learning framework to build query plans. The process is modeled as a Markov decision process, where each action is a query operation, and the goal is to maximize a cumulative reward (e.g., negative cost or negative cardinality estimate).

Datasets

The paper uses the Internet Movie Data Base (IMDB) data set from the Join Order Benchmark (JOB). They highlight that this dataset is real and includes skew and correlations across columns, making it a more challenging and realistic testbed compared to synthetic datasets like TPC-H and TPC-DS.

Key Insights and Ideas

  • Learned Representations for Subqueries: The fundamental idea is that instead of relying on hand-crafted features, deep learning can automatically learn compact and informative representations of intermediate query results. These latent vectors can capture complex data distributions and correlations that are hard to model explicitly.
  • Recursive State Transition: The recursive nature of NNST allows the model to build up representations incrementally, mirroring how query plans are constructed step-by-step. This is crucial for handling the combinatorial explosion of possible query plans.
  • Joint Training for Informative Representations: Training NNST and NNObserved together ensures that the learned state representations are not just abstract but are directly optimized to be useful for predicting key metrics like cardinality.
  • Bridging Cardinality Estimation and Plan Selection: The work proposes a unified framework where improved cardinality estimation (via learned representations) directly feeds into a reinforcement learning agent for selecting optimal query plans.

Key Results

  • Improved Cardinality Estimation: Preliminary results show that their NNModel (combining NNinit and NNST) can outperform SQL Server's cardinality estimates for selection queries, especially after a sufficient number of training epochs. For selection queries with 3 attributes, their model starts to outperform SQL Server around the 6th epoch. For 5 attributes, it takes longer to converge but still shows improvement.
  • Learning from Correlations: The model demonstrates the ability to learn from data with correlations and skew, a key advantage over traditional methods that often assume uniformity.
  • Potential for Better Plan Selection: While the paper focuses on learning representations and preliminary cardinality estimation, it lays the groundwork for using these representations with reinforcement learning to achieve better query plan enumeration.

Limitations and Future Work

  • Left-Deep Plans: The current reinforcement learning approach primarily optimizes for left-deep query plans, which are a subset of all possible plans.
  • Reward Function Design: The choice of reward function for the reinforcement learning agent is an open problem, with potential impacts on plan selection. They are experimenting with negative cardinality estimates but plan to explore negative execution times for physical plan properties.
  • Large State-Space: The state-space for query plan enumeration is inherently large. The authors acknowledge that a direct Q-learning approach is impractical and suggest learning a function to approximate QL(s, a) to generalize across states.
  • Physical Plan Properties: Future work will involve extending the learned representations to capture physical query plan properties (like memory footprint or runtime) and incorporating these into the reward function.
  • Generalizing to More Operations: The current experiments focus on selection and join operations. Extending the approach to handle a wider range of SQL operations is a natural next step.

Summary updated

BibTeX
@inproceedings{selvaraj2018learning,
  author = {Sathiya Keerthi Selvaraj and Jennifer Ortiz and Magdalena Balazinska and Johannes Gehrke and S. Sathiya Keerthi},
  title = {{Learning State Representations for Query Optimization with Deep Reinforcement Learning}},
  booktitle = {Deem@sigmod},
  year = {2018},
  doi = {10.1145/3209889.3209890},
}
Figure