Query rewriting is one of the most effective techniques for coping with poorly written queries before passing them down to the query optimizer. Manual rewriting is not scalable, as it is error-prone and requires deep expertise. Similarly, traditional query rewriting algorithms can only handle a small subset of queries: rule-based techniques do not generalize to new query patterns and synthesis-based techniques cannot handle complex queries. Fortunately, the rise of Large Language Models (LLMs), equipped with broad general knowledge and advanced reasoning capabilities, has created hopes for solving some of these previously open problems. In this paper, we present GenRewrite, the first holistic system that leverages LLMs for query rewriting. We introduce the notion of Natural Language Rewrite Rules (NLR2s), and use them as hints to the LLM but also a means for transferring knowledge from rewriting one query to another, and thus becoming smarter and more effective over time. We present a novel counterexample-guided technique that iteratively corrects the syntactic and semantic errors in the rewritten query, significantly reducing the LLM costs and the manual effort required for verification. GenRewrite speeds up 22 out of 99 TPC queries (the most complex public benchmark) by more than 2x, which is 2.5x--3.2x higher coverage than state-of-the-art traditional query rewriting and 2.1x higher than the out-of-the-box LLM baseline.
翻译:查询重写是在将查询传递给查询优化器之前处理低质量查询的最有效技术之一。人工重写不可扩展,因为其容易出错且需要深厚专业知识。同样,传统查询重写算法仅能处理一小部分查询:基于规则的方法无法泛化到新查询模式,而基于综合的方法无法处理复杂查询。幸运的是,具备广泛通用知识和高级推理能力的大语言模型的兴起,为解决这些以往开放性问题带来了希望。本文提出GenRewrite,这是首个利用大语言模型进行查询重写的整体系统。我们引入了自然语言重写规则的概念,将其作为大语言模型的提示,同时也作为将重写知识从一种查询迁移到另一种查询的手段,从而随着时间的推移提升系统智能性和有效性。我们提出一种新颖的基于反例引导的技术,可迭代修正重写查询中的语法和语义错误,显著降低大语言模型的使用成本和人工验证工作量。GenRewrite在99个TPC查询(最复杂的公开基准测试)中加速了22个查询,加速比超过2倍,其覆盖率比最先进的传统查询重写高2.5倍至3.2倍,比原始大语言模型基线高2.1倍。