Table of Contents
Fetching ...

QUITE: A Query Rewrite System Beyond Rules with LLM Agents

Yuyang Song, Hanxu Yan, Jiale Lao, Yibo Wang, Yufei Li, Yuanchun Zhou, Jianguo Wang, Mingjie Tang

TL;DR

This work tackles the problem of semantically equivalent SQL rewrites that improve performance, addressing the limitations of rule-based methods and naive LLMs. It introduces QUITE, a training-free, feedback-aware system that uses a multi-agent FSM, a rewrite middleware with a structured knowledge base and a hybrid SQL corrector, and a hint injection mechanism to optimize execution plans. The framework demonstrates significant performance gains and broader rewrite coverage across benchmarks, while maintaining high rewrite equivalence rates. QUITE's combination of data-aware reasoning, tool-assisted verification, and fine-grained hints offers a scalable path to robust query optimization in OLAP workloads.

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.

QUITE: A Query Rewrite System Beyond Rules with LLM Agents

TL;DR

This work tackles the problem of semantically equivalent SQL rewrites that improve performance, addressing the limitations of rule-based methods and naive LLMs. It introduces QUITE, a training-free, feedback-aware system that uses a multi-agent FSM, a rewrite middleware with a structured knowledge base and a hybrid SQL corrector, and a hint injection mechanism to optimize execution plans. The framework demonstrates significant performance gains and broader rewrite coverage across benchmarks, while maintaining high rewrite equivalence rates. QUITE's combination of data-aware reasoning, tool-assisted verification, and fine-grained hints offers a scalable path to robust query optimization in OLAP workloads.

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.

Paper Structure

This paper contains 25 sections, 5 equations, 14 figures, 8 tables, 1 algorithm.

Figures (14)

  • Figure 1: An Example of Using Strategies Beyond Rewrite Rules (Example \ref{['example:1']}) and Using Context-Aware Analysis (Example \ref{['example:2']})
  • Figure 2: An Example of Using Query Intent (Example \ref{['example:3']})
  • Figure 3: System Overview of QUITE
  • Figure 4: Reasoning Chain of MDP-based Reasoning Agent
  • Figure 5: The Workflow of FSM-based Query Rewrite
  • ...and 9 more figures

Theorems & Definitions (5)

  • Example 1
  • Example 2
  • Example 3
  • definition 1: Query Rewrite
  • definition 2: Query Equivalence