Fine-tune LLMs with execution plans and RL to rewrite SQL that runs faster while staying correct.

August 12, 20258 min

Overview

Decision SnapshotReady For Pilot

Results use public benchmarks and multiple ablations. Evidence is strong on PostgreSQL v14 and selected workloads, but system and optimizer differences may affect real deployments.

Citations0

Evidence Strength0.80

Confidence0.80

Risk Signals10

Trust Signals

Findings with numeric evidence: 4/4

Findings with evidence refs: 4/4

Results with explicit delta: 6/6

Reproducibility

Status: Partial assets available

Open source: Partial

At A Glance

Cost impact: 70%

Production readiness: 70%

Novelty: 65%

Authors

Dongjie Xu, Yue Cui, Weijie Shi, Qingzhi Ma, Hanghui Guo, Jiaming Li, Yao Zhao, Ruiyuan Zhang, Shimin Di, Jia Zhu, Kai Zheng, Jiajie Xu

Links

Abstract / PDF / Data

Why It Matters For Business

E3-Rewrite can cut query costs by reducing runtime for heavy analytical queries while keeping results correct. That directly lowers compute bills and improves interactive analytics latency for complex queries.

Who Should Care

Summary TLDR

E3-Rewrite fine-tunes large language models with execution-aware prompts and reinforcement learning to produce SQL rewrites that are executable, semantically equivalent, and faster. Key ingredients: prepend EXPLAIN plan text to prompts, retrieve similar successful rewrites, and train with a two-stage RL curriculum that first enforces correctness then optimizes latency.

Problem Statement

Rule-based SQL rewriting is brittle and hard to extend. LLMs can express richer rewrites but often produce non-executable or non-equivalent SQL and can hurt performance. We need a learnable system that reliably generates executable, equivalent, and faster SQL.

Main Contribution

E3-Rewrite: end-to-end LLM-based SQL rewriting that optimizes for executability, equivalence, and efficiency without hand-coded rules.

Execution-hint prompts: prepend linearized EXPLAIN / EXPLAIN ANALYZE plans so the model sees operator choices and bottlenecks.

Key Findings

Big average latency reduction on TPC-H.

NumbersAvg latency 78.81s -> 29.67s (Original vs E3-Rewrite Qwen, Table 1)

Practical UseOn complex decision-support queries, deploying plan-aware LLM rewrites can cut average runtime dramatically. Try on heavy queries first.

Evidence RefTable 1

High correctness while improving many queries.

Numbers210 improved queries and 99.6% equivalence on TPC-H (Qwen 32B, Table 2)

Practical UseWith RL and validation, the system keeps results correct for nearly all rewrites on evaluated benchmarks; include automated equivalence checks before deployment.

Evidence RefTable 2

Results

MetricValueBaselineDeltaSplit / DatasetEvidenceEvidence Ref
TPC-H average latency (Original)78.81s-> 29.67s with E3-Rewrite (Qwen)TPC-H (10GB)Table 1: Original vs E3-Rewrite (Qwen)Table 1
TPC-H p90 latency300.00s (Original)-> 51.37s with E3-Rewrite (Qwen)TPC-H (10GB)Table 1 p90Table 1

What To Try In 7 Days

Run EXPLAIN for a few slow, important queries and prepend the plan text to an LLM prompt to see whether simple plan-aware edits help.

Collect a small demo pool of original+good-rewrite pairs and try retrieval-based prompting to reuse patterns.

Add a post-check that compares outputs between original and rewritten queries on sample DB instances before deploying rewrites.

Agent Features

Memory
dynamic demonstration pool (online updates of rewrite pairs)
Planning
curriculum: correctness → performance
Tool Use
uses EXPLAIN / EXPLAIN ANALYZE as external tooluses QEDsolver for formal equivalence checks
Frameworks
GRPO
Architectures
GRPOtwo-stage curriculum learning

Optimization Features

Training Optimization
GRPOcurriculum-guided two-stage trainingrehearsal of Stage 1 examples during Stage 2
Inference Optimization
prepend linearized execution plans to promptsretrieve top-k hybrid demonstrations (structure + semantics)

Reproducibility

Code AvailableNo
Data AvailableYes
Open Source StatusPartial
LicenseUnknown

Data URLs

TPC-H (public)IMDB / JOB (public)DSB (public)

Risks & Boundaries

Limitations

Experiments run on PostgreSQL v14; optimizer behavior may differ across DBMSs.

Equivalence checking relies on QEDsolver + LLM fallbacks; solver timeouts force heuristic decisions.

When Not To Use

When formal proof of semantic equivalence is mandatory and solver support is required for all constructs.

On DBMSs that do not expose comparable EXPLAIN plans or have very different optimizer IDs.

Failure Modes

LLM generates syntactically valid but semantically different SQL; fallback checks may be inconclusive.

Optimizer cost estimates mislead the performance reward, causing rewrites that appear better to EXPLAIN but worsen real latency.

Core Entities

Models

Qwen (Qwen 14B)Qwen (Qwen 32B)LLaMA4GPT-4o

Metrics

Query LatencyEquivalence RatioImproved Queries (>=10% speedup)

Datasets

TPC-HIMDB (JOB)DSB

Benchmarks

TPC-HIMDB (JOB)DSB