用大模型生成更高效且等价的SQL,解决传统规则方法的局限。
E3-Rewrite: Learning to Rewrite SQL for Executability, Equivalence,and Efficiency
- 基于大模型和执行计划构建上下文提示,引导推理时重写。
- 在多个基准上实现最高25.6%的执行时间缩短,等价重写率提升24.4%。
- 适合数据库优化、AI辅助开发等场景,尤其擅长复杂查询优化。
SQL查询重写旨在以保持语义等价的前提下优化查询效率。现有方法多依赖预定义重写规则,但存在两大局限:(1)规则集固定,难以泛化到新查询模式,对复杂查询优化能力弱;(2)大量有效重写策略无法由声明式规则完全覆盖。为此,我们提出使用大语言模型(LLMs)生成重写。尽管LLMs能捕捉评估重排序、CTE重写等复杂策略,但直接应用常导致性能下降或非等价重写,因缺乏执行感知与语义锚定。为此,我们提出E3-Rewrite框架,实现可执行、等价且高效的SQL重写。其核心包括:(1)上下文构建模块,利用执行计划与检索示例生成瓶颈感知提示,指导推理阶段重写;(2)设计三重奖励函数,分别评估可执行性、等价性和效率,通过语法检查、等价验证与成本估算实现;(3)采用分阶段课程学习,先聚焦可执行性与等价性,再逐步引入效率目标。在多个SQL基准测试中,实验表明E3-Rewrite相比领先基线,查询执行时间最多缩短25.6%,同时满足严格等价标准的重写数量提升达24.4%。这些优势延伸至以往方法难以有效优化的复杂查询模式。
原文摘要 · Abstract (English)
SQL query rewriting aims to reformulate a query into a more efficient form while preserving equivalence. Most existing methods rely on predefined rewrite rules. However, such rule-based approaches face fundamental limitations: (1) fixed rule sets generalize poorly to novel query patterns and struggle with complex queries; (2) a wide range of effective rewriting strategies cannot be fully captured by declarative rules. To overcome these issues, we propose using large language models (LLMs) to generate rewrites. LLMs can capture complex strategies, such as evaluation reordering and CTE rewriting. Despite this potential, directly applying LLMs often results in performance regressions or non-equivalent rewrites due to a lack of execution awareness and semantic grounding. To address these challenges, We present E3-Rewrite, an LLM-based SQL rewriting framework that produces executable, equivalent, and efficient queries. It integrates two core components: a context construction module and a reinforcement learning framework. First, the context module leverages execution plans and retrieved demonstrations to build bottleneck-aware prompts that guide inference-time rewriting. Second, we design a reward function targeting executability, equivalence, and efficiency, evaluated via syntax checks, equivalence verification, and cost estimation. Third, to ensure stable multi-objective learning, we adopt a staged curriculum that first emphasizes executability and equivalence, then gradually incorporates efficiency. Across multiple SQL benchmarks, our experiments demonstrate that E3-Rewrite can shorten query execution time by as much as 25.6% relative to leading baselines, while also producing up to 24.4% more rewrites that meet strict equivalence criteria. These gains extend to challenging query patterns that prior approaches could not effectively optimize.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。