TL;DR
Integrating Large Language Models (LLMs) into databases introduces a massive challenge: structural query decisions now dictate both dollar cost and answer accuracy. This paper introduces EnumGRPO, a self-improving query optimizer that interleaves planning with execution and uses in-context reinforcement learning to discover cost-efficient, accurate workflows. It achieves a staggering 317× cost reduction over hybrid baselines while simultaneously boosting answer accuracy by 18%.
What Problem Does This Paper Address?
Classical query optimization relies on a fundamental assumption: different execution plans (such as pushing a filter down or changing a join type) are algebraically equivalent. They yield the exact same answer, meaning the optimizer only needs to minimize cost.
However, modern hybrid databases integrate LLM-backed semantic operators to handle messy data, fill knowledge gaps, or resolve ambiguities. Because LLMs are approximate and highly expensive, this classical assumption completely breaks down. A structural decision—like whether to filter rows using standard SQL before or after calling an LLM—drastically changes both the final correctness of the answer and the token bill.
Furthermore, the optimal strategy often depends on runtime data distributions that cannot be predicted statically. The authors address this by formalizing agentic query execution, a paradigm where planning and execution are interleaved, transforming query optimization into an adaptive agent workflow problem.
Methods
The authors propose EnumGRPO, a learned optimizer that operates via in-context reinforcement learning (ICRL). Instead of updating model weights (which is computationally expensive and impossible for closed-source API models), EnumGRPO refines the system prompt with human-readable natural language heuristics.
The method consists of two main phases:
- Learning Stage: For a batch of training queries, a PlanEnumerator generates $N$ diverse candidate workflows across five orthogonal axes: execution paradigm (data-driven vs. code-driven rule synthesis), operator type (scalar vs. aggregate), operator placement (pre- vs. post-aggregation), selectivity scope (full vs. targeted), and projection width (narrow vs. wide). These workflows are executed in parallel and scored using a multi-objective reward that combines execution accuracy, tuple-level F1, and cell-level F1 against a cost penalty. Group Relative Policy Optimization (GRPO) normalizes these rewards within the query group to isolate the impact of structural choices. Finally, an experience distillation pipeline converts these advantages into generalizable text guidelines stored in an experience pool ($\epsilon$).
- Query Time: The learned experience pool is simply prepended to the agent's prompt. The agent executes the query adaptively step-by-step, utilizing these heuristics without needing any runtime plan enumeration.
Datasets
The system is evaluated on SWAN, a cross-domain benchmark consisting of 120 beyond-database questions across four real-world SQLite databases derived from the BIRD benchmark:
- California Schools (3 tables)
- European Football (8 tables)
- Formula One (14 tables)
- Superhero (10 tables)
To ensure queries strictly require semantic reasoning, columns containing missing world knowledge (e.g., matching a superhero to their publisher) were intentionally dropped, forcing the system to rely on LLM inference. The 120 questions were split into 40 for learning and 80 for evaluation.
Key Insights and Ideas
- Decoupling Optimization from Weights: By using ICRL, the optimization logic lives entirely within the prompt context rather than model weights. This makes the optimizer completely portable across model generations and compatible with closed-source frontier models served via APIs.
- The Power of Group Relativity: Standard reinforcement learning struggles with database queries because a 10-row table and a 10,000-row table have wildly different baseline costs. Group-relative normalization (GRPO) elegantly cancels out query-level difficulty, focusing purely on whether a specific structural choice (like code-driven synthesis) was better than another for that specific query.
- Cross-Schema Generalization: Heuristics learned on one database schema successfully transfer to entirely different schemas. This proves that the agent learns fundamental database principles (such as "always SQL-filter to the smallest candidate set before any LLM call") rather than just memorizing facts.
Key Results
- Accuracy Boost: EnumGRPO achieved the highest overall execution accuracy at 35.4%, outperforming Agentic Text2SQL (19.2%) and Agentic BlendSQL (30.0%).
- Massive Cost Reduction: It reduced LLM-operator costs to just $0.011 per query—a 317× reduction compared to the hybrid query baseline BlendSQL ($3.42) and a 72% reduction over the unoptimized agentic baseline ($0.039).
- Efficiency Gains: LLM operator token consumption dropped by 64.4%, and average latency was cut from 235 seconds (base agent) to 153 seconds, as the learned heuristics allowed the agent to converge on a strategy faster without trial-and-error.
Limitations and Future Work
- Conservative Routing at Scale: In scalability tests on larger datasets, the agent occasionally became overly conservative, opting for pure SQL fallbacks to save costs, which sometimes caused it to skip useful semantic evidence.
- Heuristic Management: As the experience pool grows, managing and retrieving the most relevant heuristics for a given query will become a bottleneck. The authors suggest future work should explore retrieving experiences dynamically based on relevance.
- Continual Learning: The current setup separates learning from execution. Extending this to post-deployment continual learning would allow the system to adapt to live workload and data shifts.
Summary updated