Skip to main content
QUICK REVIEW

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

Zhaodonghui Li, Haitao Yuan|arXiv (Cornell University)|Apr 19, 2024
Advanced Database Systems and QueriesComputer Science3 citations
TL;DR

LLM-R2 proposes a large language model (LLM)-enhanced rule-based query rewrite system that uses contrastive learning to select effective query demonstrations, enabling accurate and efficient rewrite rule recommendations. By leveraging LLM reasoning while preserving query equivalence and executability, LLM-R2 significantly improves query execution efficiency across diverse datasets, outperforming baseline methods in both rule diversity and execution speed.

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.

Motivation & Objective

  • To address the limitations of traditional rule-based query rewrite systems, which suffer from high computational costs and reliance on inaccurate DBMS cost estimators.
  • To overcome the hallucination and correctness issues inherent in end-to-end LLM-based query rewriting by integrating LLMs with a rule-based database system.
  • To improve the efficiency and effectiveness of query rewrite by using LLMs to suggest optimal rewrite rule strategies, rather than generating queries directly.
  • To enhance LLM inference in rule recommendation through contrastive learning and curriculum-based demonstration selection for better generalization.
  • To achieve high robustness and transferability across different datasets, model backbones, and query workloads.

Proposed method

  • LLM-R2 employs a two-stage framework: first, it uses a contrastive model trained via curriculum learning to embed queries and select the most effective demonstrations for prompting the LLM.
  • The selected demonstrations are used to prompt the LLM to suggest a sequence of rewrite rules that maintain query equivalence while improving execution efficiency.
  • The system integrates with a standard database platform (e.g., Apache Calcite) to apply the suggested rewrite rules, ensuring syntactic correctness and result equivalence.
  • A contrastive loss function is used to align query representations such that semantically similar queries are embedded closely, improving demonstration relevance.
  • The method uses few-shot prompting (1-shot) to balance inference efficiency and rewrite quality, avoiding the performance drop seen in 3-shot prompting.
  • The framework is designed to be model-agnostic and transferable, supporting various LLM backbones and datasets.

Experimental results

Research questions

  • RQ1Can an LLM effectively suggest rewrite rule strategies that improve query execution efficiency while maintaining equivalence and executability?
  • RQ2How does contrastive demonstration selection enhance the LLM’s ability to recommend accurate and effective rewrite rules?
  • RQ3What is the optimal few-shot prompting strategy (e.g., 1-shot vs. 3-shot) for balancing rewrite quality and inference cost?
  • RQ4How does LLM-R2 perform in comparison to rule-based baselines and end-to-end LLM methods across diverse datasets and query types?
  • RQ5To what extent can the LLM-enhanced system generalize across different database schemas, query workloads, and model architectures?

Key findings

  • LLM-R2 achieved a 92.16% success rate in generating efficient rewrites, with 235 out of 255 proposals improving execution time in the 3-shot prompting setup.
  • The 1-shot prompting strategy outperformed 3-shot prompting due to reduced inconsistency in demonstration guidance and lower latency.
  • LLM-R2 recommended 56 unique rewrite rules across TPC-H, IMDB, and DSB datasets, significantly outperforming the LR baseline in rule diversity and application frequency.
  • The system demonstrated high robustness and transferability across different datasets, model backbones, and query scales, indicating strong generalization capability.
  • Despite higher rewrite latency than pure DB methods, the time savings from improved query execution more than compensate, making the overall process efficient.
  • LLM-R2 successfully identified optimization opportunities missed by the LR baseline, including complex rule sequences like transforming a 'WHERE' clause into an 'INNER JOIN' via 'FILTER_INTO_JOIN'.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.