E3-Rewrite: Learning to Rewrite SQL for Executability, Equivalence,and Efficiency

📅 2025-08-12
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
Existing rule-based SQL rewriting approaches suffer from poor generalizability and fail to handle complex queries or capture implicit optimization strategies. To address this, we propose the first LLM-based SQL rewriting framework jointly driven by execution awareness and semantic alignment. Our method constructs context-aware prompts using query execution plans and exemplars, integrates syntax validation, semantic equivalence verification, and cost estimation modules, and employs a three-stage curriculum-style reinforcement learning strategy to jointly optimize for execution time, semantic equivalence, and rewriting success rate. Evaluated on multiple benchmarks, our framework achieves an average 25.6% reduction in execution time and a 24.4% improvement in rewriting success rate, demonstrating significantly enhanced modeling and optimization capability for complex SQL queries.

Technology Category

Application Category

📝 Abstract
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 suboptimal 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. Extensive experiments show that E3-Rewrite achieves up to a 25.6% reduction in query execution time compared to state-of-the-art methods across multiple SQL benchmarks. Moreover, it delivers up to 24.4% more successful rewrites, expanding coverage to complex queries that previous systems failed to handle.
Problem

Research questions and friction points this paper is trying to address.

Overcoming limitations of rule-based SQL query rewriting methods
Ensuring executable, equivalent, and efficient SQL rewrites using LLMs
Addressing suboptimal rewrites via execution-aware context and reinforcement learning
Innovation

Methods, ideas, or system contributions that make the work stand out.

LLM-based SQL rewriting for efficiency and equivalence
Context module with execution-aware prompting
Reinforcement learning with staged curriculum
🔎 Similar Papers
D
Dongjie Xu
Soochow University
Y
Yue Cui
Hong Kong University of Science and Technology
Weijie Shi
Weijie Shi
Hong Kong University of Science and Technology
Q
Qingzhi Ma
Soochow University
Hanghui Guo
Hanghui Guo
Zhejiang Normal University
Large Language Model
J
Jiaming Li
ByteDance Inc.
Y
Yao Zhao
Alibaba Group
Ruiyuan Zhang
Ruiyuan Zhang
Zhejiang University
MultiModal3D Part AssemblyMixture-of-Expert
S
Shimin Di
Southeast University
Jia Zhu
Jia Zhu
Zhejiang Normal University
Artificial IntelligenceKnowledge GraphData QualityComputational Pedagogy
K
Kai Zheng
University of Electronic Science and Technology of China
J
Jiajie Xu
Soochow University