arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2606.08620cs.DB
收藏

SPA:一种面向LLM查询重写的SQL计划感知强化学习框架

SPA: A SQL-Plan-Aware Reinforcement Learning Framework for Query Rewriting with LLMs

Simon Fraser University(西蒙菲莎大学)

Xinyi Huang, Zhengjie Miao

AI总结:

提出SPA框架,利用物理执行反馈训练LLM重写SQL查询,通过概率门控自适应奖励塑形和策略自改进,显著提升端到端运行时性能并减少有害重写。

摘要:

SQL查询重写是一种无需修改模式或索引即可提升数据库性能的成熟技术,但为现代分析工作负载找到有效的重写仍然困难:基于规则的方法局限于预定义转换,而基于LLM的方法通常产生语义有效但编译为等效物理计划或降低运行时性能的重写。我们提出SPA,一个SQL计划感知的强化学习框架,利用物理执行反馈训练LLM重写查询。SPA将重写形式化为策略优化问题,并扩展GRPO,奖励涵盖语义等价性、文本重写距离、物理计划差异和运行时加速。为了处理跨查询难度的奖励稀疏性,SPA引入了概率门控自适应奖励塑形,一种查询级课程,仅在回滚组达到足够低层目标掌握度时才解锁高层奖励,并通过回收当前策略中的减速重写作为针对性训练信号,进一步通过策略自改进提高样本效率。在IID和OOD工作负载上,SPA在端到端运行时优于基于规则和强LLM基线,大幅减少有害减速重写,并带来显著的尾延迟增益。

英文摘要:

SQL query rewriting is a well-established technique for improving database performance without schema or index changes, yet finding effective rewrites for modern analytical workloads remains difficult: rule-based methods are limited to predefined transformations, while LLM-based approaches often produce rewrites that are semantically valid but compile to equivalent physical plans or degrade runtime performance. We present SPA, a SQL-Plan-Aware reinforcement learning framework that trains LLMs to rewrite queries using physical execution feedback. SPA formulates rewriting as a policy optimization problem and extends GRPO with rewards spanning semantic equivalence, textual rewrite distance, physical-plan divergence, and runtime speedup. To handle reward sparsity across query difficulty, SPA introduces Probability-Gated Adaptive Reward Shaping, a query-level curriculum that unlocks higher-level rewards only once a rollout group achieves sufficient mastery of lower-level objectives, and further improves sample efficiency through on-policy self-improvement by recycling slowdown rewrites from the current policy as targeted training signals. On both IID and OOD workloads, SPA outperforms rule-based and strong LLM baselines in end-to-end runtime, substantially reduces harmful slowdown rewrites, and yields strong tail-latency gains.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑