QUITE: A Query Rewrite System Beyond Rules with LLM Agents

๐Ÿ“… 2025-06-09
๐Ÿ“ˆ Citations: 0
โœจ Influential: 0
๐Ÿ“„ PDF
๐Ÿค– AI Summary
Existing SQL query rewriting approaches rely on predefined rules, encountering three key bottlenecks: difficulty in discovering effective rules, poor generalizability, and inability to express complex optimization logicโ€”leading to narrow coverage and frequent performance regressions. This paper proposes the first training-free, feedback-aware LLM-based multi-agent framework for dynamic query rewriting. It integrates a finite-state-machine-driven workflow controller, a database execution feedback loop, a rewriting middleware layer, and prompt injection techniques to ensure both semantic equivalence and high-performance execution. Experimental results demonstrate that, compared to state-of-the-art methods, our approach achieves up to a 35.8% reduction in query execution time and a 24.1% improvement in rewriting success rate. Moreover, it significantly broadens support for complex query patterns and diverse rewriting strategies.

Technology Category

Application Category

๐Ÿ“ Abstract
Query rewrite transforms SQL queries into semantically equivalent forms that run more efficiently. Existing approaches mainly rely on predefined rewrite rules, but they handle a limited subset of queries and can cause performance regressions. This limitation stems from three challenges of rule-based query rewrite: (1) it is hard to discover and verify new rules, (2) fixed rewrite rules do not generalize to new query patterns, and (3) some rewrite techniques cannot be expressed as fixed rules. Motivated by the fact that human experts exhibit significantly better rewrite ability but suffer from scalability, and Large Language Models (LLMs) have demonstrated nearly human-level semantic and reasoning abilities, we propose a new approach of using LLMs to rewrite SQL queries beyond rules. Due to the hallucination problems in LLMs, directly applying LLMs often leads to nonequivalent and suboptimal queries. To address this issue, we propose QUITE (query rewrite), a training-free and feedback-aware system based on LLM agents that rewrites SQL queries into semantically equivalent forms with significantly better performance, covering a broader range of query patterns and rewrite strategies compared to rule-based methods. Firstly, we design a multi-agent framework controlled by a finite state machine (FSM) to equip LLMs with the ability to use external tools and enhance the rewrite process with real-time database feedback. Secondly, we develop a rewrite middleware to enhance the ability of LLMs to generate optimized query equivalents. Finally, we employ a novel hint injection technique to improve execution plans for rewritten queries. Extensive experiments show that QUITE reduces query execution time by up to 35.8% over state-of-the-art approaches and produces 24.1% more rewrites than prior methods, covering query cases that earlier systems did not handle.
Problem

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

Overcoming limitations of rule-based SQL query rewrite systems
Using LLMs to rewrite SQL queries beyond fixed rules
Ensuring semantic equivalence and performance in LLM-based query rewrites
Innovation

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

LLM agents rewrite SQL queries beyond rules
Multi-agent framework with real-time feedback
Hint injection improves execution plans