TL;DR
JoinGym is a lightweight, ultra-fast offline simulator that lets researchers train reinforcement learning (RL) agents for database query optimization without setting up slow, live database systems. By replacing live query executions with instant lookups from a massive pre-computed dataset of 3,300 IMDb queries, JoinGym accelerates training throughput by orders of magnitude and exposes critical real-world RL challenges like heavy-tailed rewards and partial observability.
What Problem Does This Paper Address?
Database query optimization relies heavily on Join Order Selection (JOS)—the NP-hard problem of finding the most efficient sequence to join tables. Get it right, and a query finishes in milliseconds; get it wrong, and it can run for days.
While deep reinforcement learning shows massive promise in solving JOS, existing research environments require a live database management system (DBMS) backend (like PostgreSQL). Executing queries online to calculate rewards takes seconds to hours per step, making RL training prohibitively slow, expensive, and inaccessible for most research labs. JoinGym addresses this bottleneck by moving the entire simulation offline.
Methods
The authors formulate JOS as a Partially Observable Contextual Markov Decision Process (POCMDP). To bypass live database execution, JoinGym simulates a query plan's cost by looking up the exact sizes of intermediate tables (intermediate result cardinalities) from a pre-computed offline dataset.
JoinGym supports both left-deep plans (joining base tables sequentially) and bushy plans (joining intermediate trees), and allows researchers to enable or disable Cartesian products (joining tables without matching column constraints). The environment uses action masking to ensure agents only select valid remaining joins at each step.
Datasets
The paper introduces a massive new dataset containing all intermediate result cardinalities for 3,300 SQL queries based on real workloads from the Internet Movie Database (IMDb). This dataset is built on 33 templates from the standard Join Order Benchmark (JOB) but is 30x larger and significantly more diverse. Because table sizes are deterministic and system-agnostic, this pre-computed dataset allows JoinGym to simulate thousands of query trajectories per second on a standard laptop.
Key Insights and Ideas
- Cardinality as the Ultimate Proxy: Minimizing intermediate table sizes is the single most critical factor in query optimization. While runtime metrics are system-dependent and noisy, intermediate result cardinalities are deterministic and system-agnostic, making them perfect for offline simulation.
- The Nightmare of Long Tails: Query optimization costs are heavily long-tailed. A slightly sub-optimal choice can cause intermediate table sizes to blow up by many orders of magnitude, leading to catastrophic planning failures.
- Combinatorial Generalization: Queries sharing identical structures (subgraphs) can have wildly different optimal join orders based purely on their filter predicates. This makes JOS an excellent real-world testbed for how RL agents generalize across discrete combinatorial spaces.
Key Results
The authors extensively benchmarked five standard RL algorithms (DQN, DDQN, TD3, SAC, and PPO) on JoinGym:
- Left-Deep vs. Bushy: Algorithms performed uniformly better on left-deep plans because the search space is exponentially smaller.
- Cartesian Products: Disallowing Cartesian products dramatically simplified the problem and led to much better agent convergence.
- Actor-Critics Win: Off-policy actor-critic methods (TD3 and SAC) achieved the best overall performance and were far more sample-efficient than PPO and more stable than DQN.
- The Generalization Gap: While agents generalized well at the 90% quantile, their performance sharply degraded at the 95% and 99% quantiles due to the severe impact of the long-tailed cost distribution.
Limitations and Future Work
- Static Workloads: The current simulator assumes the underlying database content remains static during training and evaluation, meaning it does not capture real-time database updates or dynamic workloads.
- Duplicate Base Tables: The state encoding handles queries with multiple instances of the same base table using a fixed-position approach that the authors note is not fully elegant and could be improved.
- Call for Risk-Sensitive RL: The heavy-tailed nature of the returns suggests that standard expected-value RL is insufficient. The authors advocate for future work using risk-sensitive RL frameworks like Conditional Value-at-Risk (CVaR) to protect against worst-case query optimization failures.
Summary updated