用大模型代理系统自动优化SQL查询,效果优于传统规则方法。
QUITE: A Query Rewrite System Beyond Rules with LLM Agents
- 设计多智能体框架,结合数据库反馈动态优化重写过程。
- 实验显示查询执行时间最高降低35.8%,可重写案例多出24.1%。
- 无需训练,适合处理复杂或罕见的查询模式,适合数据库工程师。
查询重写将SQL查询转换为语义等价但运行更高效的形态。现有方法主要依赖预定义规则,但覆盖范围有限且可能引发性能下降。这一局限源于三大挑战:(1)新规则难以发现与验证,(2)固定规则无法泛化至新查询模式,(3)部分重写技术无法用固定规则表达。鉴于人类专家在重写上表现优异但难以扩展,而大语言模型(LLMs)已展现接近人类的语义理解与推理能力,我们提出一种基于LLM代理、超越规则的查询重写方法——QUITE。针对LLM幻觉问题,QUITE采用无训练、反馈感知的设计:首先构建由有限状态机控制的多智能体框架,使模型可调用外部工具并利用实时数据库反馈;其次开发重写中间件增强生成等效优化查询的能力;最后引入新型提示注入技术提升重写后查询的执行计划质量。大量实验表明,QUITE相比前沿方法最多降低35.8%的查询执行时间,重写案例数高出24.1%,覆盖此前系统无法处理的查询场景。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。