Table of Contents
Fetching ...

LLM-R2: A Large Language Model Enhanced Rule-based Rewrite System for Boosting Query Efficiency

Zhaodonghui Li, Haitao Yuan, Huiming Wang, Gao Cong, Lidong Bing

TL;DR

The paper tackles the challenge of expensive and uncertain query rewrite by integrating a large language model with a rule-based rewrite system to propose and select effective rewrite rules while preserving query equivalence. It introduces a Demonstration Manager that builds a pool of high-quality demonstrations and employs a contrastive representation model with curriculum learning to prompt the LLM with the most relevant demonstrations. Empirical results on IMDB, TPC-H, and DSB show that LLM-R2 substantially reduces execution time compared to baselines, with robust performance across datasets and scales, albeit with some additional rewrite latency. This work demonstrates a practical pathway for applying LLMs to database optimization problems by leveraging existing rewrite rules and targeted demonstration-based prompting to achieve significant efficiency gains.

Abstract

Query rewrite, which aims to generate more efficient queries by altering a SQL query's structure without changing the query result, has been an important research problem. In order to maintain equivalence between the rewritten query and the original one during rewriting, traditional query rewrite methods always rewrite the queries following certain rewrite rules. However, some problems still remain. Firstly, existing methods of finding the optimal choice or sequence of rewrite rules are still limited and the process always costs a lot of resources. Methods involving discovering new rewrite rules typically require complicated proofs of structural logic or extensive user interactions. Secondly, current query rewrite methods usually rely highly on DBMS cost estimators which are often not accurate. In this paper, we address these problems by proposing a novel method of query rewrite named LLM-R2, adopting a large language model (LLM) to propose possible rewrite rules for a database rewrite system. To further improve the inference ability of LLM in recommending rewrite rules, we train a contrastive model by curriculum to learn query representations and select effective query demonstrations for the LLM. Experimental results have shown that our method can significantly improve the query execution efficiency and outperform the baseline methods. In addition, our method enjoys high robustness across different datasets.

LLM-R2: A Large Language Model Enhanced Rule-based Rewrite System for Boosting Query Efficiency

TL;DR

The paper tackles the challenge of expensive and uncertain query rewrite by integrating a large language model with a rule-based rewrite system to propose and select effective rewrite rules while preserving query equivalence. It introduces a Demonstration Manager that builds a pool of high-quality demonstrations and employs a contrastive representation model with curriculum learning to prompt the LLM with the most relevant demonstrations. Empirical results on IMDB, TPC-H, and DSB show that LLM-R2 substantially reduces execution time compared to baselines, with robust performance across datasets and scales, albeit with some additional rewrite latency. This work demonstrates a practical pathway for applying LLMs to database optimization problems by leveraging existing rewrite rules and targeted demonstration-based prompting to achieve significant efficiency gains.

Abstract

Query rewrite, which aims to generate more efficient queries by altering a SQL query's structure without changing the query result, has been an important research problem. In order to maintain equivalence between the rewritten query and the original one during rewriting, traditional query rewrite methods always rewrite the queries following certain rewrite rules. However, some problems still remain. Firstly, existing methods of finding the optimal choice or sequence of rewrite rules are still limited and the process always costs a lot of resources. Methods involving discovering new rewrite rules typically require complicated proofs of structural logic or extensive user interactions. Secondly, current query rewrite methods usually rely highly on DBMS cost estimators which are often not accurate. In this paper, we address these problems by proposing a novel method of query rewrite named LLM-R2, adopting a large language model (LLM) to propose possible rewrite rules for a database rewrite system. To further improve the inference ability of LLM in recommending rewrite rules, we train a contrastive model by curriculum to learn query representations and select effective query demonstrations for the LLM. Experimental results have shown that our method can significantly improve the query execution efficiency and outperform the baseline methods. In addition, our method enjoys high robustness across different datasets.
Paper Structure (35 sections, 6 equations, 8 figures, 9 tables, 1 algorithm)

This paper contains 35 sections, 6 equations, 8 figures, 9 tables, 1 algorithm.

Figures (8)

  • Figure 1: A TPC-H query and its query tree
  • Figure 2: The Framework of LLM-enhanced Rewrite System
  • Figure 3: An Example of the In-Context Learning Process in LLM-R2. All the instructions are concatenated together as one string input to the LLM. In a zero-shot setting, the "Demonstration Instruction" will be removed and an input query will be appended directly after the "Rule Instruction".
  • Figure 4: Example of good and bad demonstration selections
  • Figure 5: Our demonstration preparation module generates a set of training triplets and a demonstration pool.
  • ...and 3 more figures

Theorems & Definitions (2)

  • definition 1
  • definition 2