用大模型优化SQL查询,减少延迟且不改结果。
R-Bot: An LLM-based Query Rewrite System
- 构建多源证据管道,引导大模型避免幻觉。
- 结合结构与语义检索,精准匹配重写规则。
- 支持自省式迭代重写,适合生产环境部署。
查询重写对优化SQL执行效率至关重要,且不改变结果。传统方法依赖启发式或学习模型,存在质量差、鲁棒性低的问题。大语言模型(LLM)凭借其强大的自然语言和代码理解能力提供了新思路,但直接使用如GPT-4等模型常因幻觉导致生成不准确或无关结果。为此,我们提出R-Bot,一种系统化的基于大模型的查询重写系统。首先设计多源重写证据准备流程,生成用于指导大模型的重写证据以防止幻觉;其次提出结构-语义融合的检索方法,结合结构与语义分析,高效检索最相关的重写证据;最后采用分步式大模型重写策略,通过迭代利用检索到的证据,结合自我反思选择并排列重写规则。我们在真实数据集和主流基准上进行充分实验,验证了R-Bot优于现有最优方法。该系统已在华为部署并服务真实客户,结果显示查询延迟显著降低。
原文摘要 · Abstract (English)
Query rewrite is essential for optimizing SQL queries to improve their execution efficiency without changing their results. Traditionally, this task has been tackled through heuristic and learning-based methods, each with its limitations in terms of inferior quality and low robustness. Recent advancements in LLMs offer a new paradigm by leveraging their superior natural language and code comprehension abilities. Despite their potential, directly applying LLMs like GPT-4 has faced challenges due to problems such as hallucinations, where the model might generate inaccurate or irrelevant results. To address this, we propose R-Bot, an LLM-based query rewrite system with a systematic approach. We first design a multi-source rewrite evidence preparation pipeline to generate query rewrite evidences for guiding LLMs to avoid hallucinations. We then propose a hybrid structure-semantics retrieval method that combines structural and semantic analysis to retrieve the most relevant rewrite evidences for effectively answering an online query. We next propose a step-by-step LLM rewrite method that iteratively leverages the retrieved evidences to select and arrange rewrite rules with self-reflection. We conduct comprehensive experiments on real-world datasets and widely used benchmarks, and demonstrate the superior performance of our system, R-Bot, surpassing state-of-the-art query rewrite methods. The R-Bot system has been deployed at Huawei and with real customers, and the results show that the proposed R-Bot system achieves lower query latency.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。