Query optimization—the process by which a database determines the most efficient way to retrieve and join data—is a computationally difficult task. Specifically, determining the optimal join order for a set of tables is known to be an NP-hard problem. For a query involving just three tables, there can be over 4,600 different combinations of join trees, orientations, algorithms, and scan types. While professional databases like Postgres use dynamic programming and genetic algorithms to prune this search space, they can still produce sub-optimal plans, especially as the number of joins increases.

In an experiment to address this, Rohan Bansal trained a 4B parameter model to generate Postgres query plans. The model initially struggled, failing to produce a valid plan for 99% of the test queries. However, through a multi-stage training process involving supervised fine-tuning (SFT) and agentic reinforcement learning (RL), the model eventually achieved a 44.7% latency reduction across 113 join-heavy queries compared to Postgres's default plans. The training utilized off-policy distillation from GPT-6 Astra agent trajectories and a custom GRPO (Group Relative Policy Optimization) variant. This RL approach assigned scalar rewards based on the actual execution time of the plans, nudging the model's weights toward strategies that resulted in faster query completion.

A graphic showing a 4B model producing query plans significantly faster than Postgres
The experiment demonstrated that a 4B parameter model could achieve substantial latency reductions in query execution compared to default Postgres plans.rohanbansal.com

The model achieved a 44.7% latency reduction across 113 join-heavy queries through a multi-stage training process.

Despite these results, the transition from a controlled experiment to a production environment involves significant trade-offs. The experiment was conducted on an 8GB dataset that fits entirely in memory, which may not accurately reflect how these plans perform on massive, high-concurrency OLTP (Online Transaction Processing) workloads where page cache contention and disk I/O become dominant factors. Furthermore, there is a risk of 'overfitting' to specific data shapes; if the underlying data correlations change or if the model misses a specific index due to a hallucination, the plan could fail. Ultimately, even an AI-generated plan is limited by the accuracy of the database's underlying statistics; if the data's logical ordering is not perfectly captured, even the most sophisticated optimizer may struggle to find the truly optimal path. editorial.evidence_sufficient: true,