Abstract
Traditional rule-based query rewriting systems often struggle with complex, unseen patterns and lack the expressiveness to implement expert-level optimization strategies. We present QUITE, a training-free, multi-agent framework that leverages Large Language Models (LLMs) to perform sophisticated SQL rewrites. QUITE employs a Finite State Machine (FSM) to coordinate specialized agents for reasoning, rewriting, verification, and decision-making. By integrating a structured knowledge base, a hybrid SQL corrector, and a query hint recommender, QUITE achieves significant performance gains—reducing execution time by up to 81.7% across benchmarks—while ensuring high semantic equivalence and handling complex human-written SQL.
Introduction
Database performance is critically dependent on query rewriting: the process of transforming a slow SQL query into a semantically identical but more efficient version. Conventional systems rely on predefined, human-coded rules, such as predicate pushdown or join reordering. However, these rule-based systems face three major hurdles: inflexibility toward unseen query patterns, limited expressiveness for complex strategies like subquery-to-CTE conversion, and a lack of context awareness regarding specific data distributions.
While Large Language Models (LLMs) offer powerful reasoning capabilities that could address these gaps, they are prone to "hallucinations," often generating SQL that is syntactically incorrect or semantically divergent. QUITE (Query Rewrite) is designed to harness LLM reasoning within a strictly enforced, multi-agent framework that ensures both performance and correctness.
Methods
QUITE is structured as a multi-agent system managed by a Finite State Machine (FSM). The architecture consists of four primary agents that collaborate to iteratively refine queries:
- Reasoning Agent: Formulates optimization proposals using a Markov Decision Process (MDP) approach, treating rewriting as a sequence of actions aimed at minimizing the estimated execution cost.
- Rewrite Agent: Translates high-level reasoning proposals into concrete, executable SQL code.
- Assistant Agent: Performs rigorous syntax checks and semantic equivalence verification using formal tools like SQLSolver.
- Decision Agent: Analyzes a multi-dimensional report—including cost estimates, resource usage, and execution plan changes—to determine whether to accept a rewrite or trigger another iteration.
The system is grounded by a Rewrite Middleware that includes a structured knowledge base of 241 high-quality optimization units and a hybrid SQL corrector. Furthermore, QUITE bridges the gap between logical and physical optimization through a Query Hint Recommender. This component injects physical-level hints (e.g., /*+ HashJoin(...) */) to guide the database engine's execution plan, preventing the optimizer from making poor choices after a logical rewrite.
Evaluation and Results
QUITE was evaluated on four diverse benchmarks: TPC-H, DSB (Data Set Benchmark), Calcite, and a new "StackOverflow-Math" dataset consisting of complex, real-world human SQL.
- Performance: QUITE reduced query execution time by 31.9% to 81.7% across benchmarks compared to traditional rule-based and recent ML-based baselines. In specific complex cases within the DSB benchmark, CTE-based rewrites led to a 37.5x reduction in latency.
- Coverage and Reliability: The system successfully produced 24.1% more valid rewrites than previous state-of-the-art methods, particularly on complex queries that rule-based systems could not handle.
- Semantic Equivalence: Despite the use of LLMs, the system maintained a near-perfect equivalence rate—100% on TPC-H and 96.8% on DSB—thanks to the hybrid corrector and multi-agent verification loop.
Conclusion and Future Work
QUITE demonstrates that LLM agents can move query optimization beyond the limitations of static rules. By combining flexible reasoning with formal verification and physical hint injection, the system provides a robust solution for optimizing long-running analytical (OLAP) queries. Future work will focus on reducing the latency of the rewrite process itself and adapting the hint injection mechanism to a wider variety of database engines beyond PostgreSQL.
Summary updated