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

Authors: Dongjie Xu, Weijie Shi, Yue Cui, Qingzhi Ma, Wenwen Xia, Ruiyuan Zhang, Shimin Di, Jia Zhu, Kai Zheng, Jiajie Xu

Abstract

SQL query rewriting reformulates queries into more efficient forms while preserving equivalence. Most existing methods rely on predefined rules that struggle to generalize beyond fixed query patterns and cannot express complex rewriting strategies. Large language models (LLMs) can reason over complex structures and generalize to diverse patterns, but their limited execution awareness and semantic grounding often result in non-equivalent or inefficient rewrites. To this end, we propose E3-Rewrite, an LLM-based framework that generates executable, equivalent, and efficient SQL rewrites. It features two key designs: (1) an execution-aware context construction that integrates query plans and retrieved examples to expose structural bottlenecks; and (2) a reinforcement learning optimization with executability, equivalence, and efficiency rewards, trained under a correctness-to-efficiency curriculum for stable convergence. Experiments on multiple benchmarks show that E3-Rewrite-Rewrite reduces execution time by up to 25.6% over leading baselines and delivers 7.3% more equivalent rewrites, extending SQL rewriting to query patterns previously beyond rule-based methods.

PDFOpen original
Find similar papers with AI

Copy this prompt into your assistant

Using Paperlist, find papers similar to "E3-Rewrite: Learning to Rewrite SQL for Executability, Equivalence, and Efficiency" (id: 239294, EMNLP 2026).
set up https://paperlist.ai/SKILL.md

MCP setupCLI