LLM-based SQL Query Optimization through Meta-prompting with Verifiable Query Equivalence
Abstract
Recent advances in Large Language Models (LLMs) have enabled large strides in optimizing SQL query execution and optimizations. LLM-based query rewriting to find leaner and more efficient queries for frequent or heavy workloads remains challenging because of strict requirements in query equivalency, which are hard to met with LLMs due to them being prone to hallucinations or suboptimal rewrites. We propose Meta-prompted Automated Rewriting of Queries (MARQ), a framework that autonomously improves SQL rewrite prompts through an iterative meta-prompting loop. MARQ generates candidate rewrites, validates their semantic equivalence and performance improvement, and refines the rewriting prompt based on execution outcomes and failure/success analyses. Finally, MARQ produces a prompt that rewrites queries in a single LLM pass, which are them checked for semantic equivalence and expected speedups before being approved. We evaluate MARQ against baselines in two public benchmarks (TPC-DS and STATS) and three real-world customer databases, and show that MARQ overperforms the baselines under our strict equivalence requirements. Our result indicates meta-prompting is a promising direction that could be explored in many database optimization tasks.