Can Large Language Models Be Query Optimizer for Relational Databases?

Jie Tan, Kangfei Zhao, Rui Li, Jeffrey Xu Yu, Chengzhi Piao, Hong Cheng, Helen Meng, Deli Zhao, Yu Rong · OpenReview

TL;DR

This paper explores using Large Language Models (LLMs) for relational database query optimization, a task traditionally handled by complex, heuristic-driven optimizers. They propose LLM-QO, a framework that fine-tunes LLMs to directly generate optimized query execution plans, demonstrating competitive and sometimes superior performance compared to traditional and learned optimizers.

What Problem Does This Paper Address?

Query optimization is a critical component of database management systems (DBMS). Its goal is to find the most efficient execution plan for a given SQL query. Traditional optimizers rely on complex cost models and heuristics, which can lead to suboptimal plans due to the vast and exponentially growing plan space. While machine learning has been explored to improve query optimization, this paper investigates a novel approach: leveraging the advanced reasoning and planning capabilities of LLMs to directly generate query plans, bypassing the traditional "plan enumeration and search" paradigm. The challenge lies in enabling LLMs, which are general-purpose models, to understand the intricacies of query planning, database schemas, and operator costs to produce valid and efficient plans.

Methods

The core of the proposed solution is LLM-QO, a framework that fine-tunes general-purpose LLMs for query optimization. This involves two main stages:

  • QINSTRUCT Data Recipe: A novel data preparation pipeline that serializes database metadata, SQL queries, and corresponding execution plans into a textual format suitable for LLM input. This recipe includes the SQL query, optimization instructions, auxiliary database information (schema, statistics), and a planning demonstration to guide the LLM.
  • Two-Stage Fine-Tuning Pipeline:
    • Query Instruction Tuning (QIT): A supervised fine-tuning stage where the LLM learns to generate valid execution plans by imitating existing optimizers. This stage uses a dataset of (prompt, plan) pairs.
  • Query Direct Preference Optimization (QDPO): A second stage that refines the LLM's ability to generate efficient plans. It uses preference data (pairs of good vs. ordinary plans) to train the LLM to favor better execution strategies, inspired by reinforcement learning from human feedback but implemented more directly.
  • LoRA (Low-Rank Adaptation): A parameter-efficient fine-tuning technique is used to manage the computational cost of fine-tuning large LLMs.

Datasets

The paper uses the following datasets for training and evaluation:

  • IMDB: A relational dataset with 6 relations and 18 attributes.
  • TPC-DS: A benchmark dataset with 24 relations, used here as the basis for the DSB (Decision Support Benchmark) query set.
  • JOB-light: A dataset derived from the Join Order Benchmark, comprising hand-written join queries suitable for templates.

The authors also introduce the QINSTRUCT data recipe, which is a methodology for preparing training data rather than a dataset itself.

Key Insights and Ideas

  • LLMs as Direct Plan Generators: The paper demonstrates that LLMs can be trained to directly generate query execution plans, shifting from traditional search-based optimization to a sequence generation paradigm.
  • QINSTRUCT's Importance: The structured data recipe (QINSTRUCT) is crucial for providing LLMs with the necessary context (schema, statistics, examples) to understand and perform query optimization.
  • Two-Stage Tuning is Key: The combination of QIT (for validity) and QDPO (for efficiency) is essential for achieving high-quality plans. QDPO significantly improves performance by learning preferences between good and bad plans.
  • Demonstration and Planning Path Matter: The planning demonstration and the structured "planning path" representation within QINSTRUCT are vital for reducing invalid plan generations, especially in out-of-distribution scenarios.
  • Adaptability of QDPO: The QDPO stage shows promise in adapting to new optimizer preferences, allowing LLM-QO to learn from diverse sources and potentially become a "best overall optimizer."

Key Results

  • LLM-QO (QDPO) achieved significant improvements in average execution time compared to PostgreSQL: 5.6% on IMDB, 8.6% on JOB-light, and 68.7% on DSB.
  • LLM-QO consistently outperformed traditional optimizers (PostgreSQL, Oracle) and learned optimizers (Bao, HybridQO) across various query workloads, especially in long-tail scenarios (95th and 99th quantiles).
  • For out-of-distribution (OOD) queries, LLM-QO (QIT) showed strong generalization, outperforming PostgreSQL by a statistically significant margin.
  • Ablation studies confirmed the importance of input features (statistics, demonstration) and the output format (planning path) for LLM-QO's performance.
  • Case studies revealed that LLM-QO can generate plans with different join orders that lead to better actual execution times than those chosen by traditional optimizers, even when those optimizers claim lower estimated costs.

Limitations and Future Work

  • Generalization to Diverse Databases/Engines: The current LLM-QO is trained for a specific database instance and execution engine (PostgreSQL). Future work aims to enable generalization across multiple database instances, execution engines, and query types.
  • Inference Efficiency: While LLM-QO shows promise, improving inference speed is crucial for practical deployment. Techniques like KV caching and speculative decoding are mentioned as potential solutions.
  • Hallucination and Coherence: LLMs can still exhibit hallucination. Future work will focus on refining prompts and generation processes to ensure plans are coherent and adhere to necessary conditions.
  • Adding More Optimizers: The paper suggests that incorporating plans from more optimizers in the QDPO stage can further enhance adaptability and performance.
  • Complex Query Types: The current work focuses on SPJ queries. Extending to more complex query types is a natural next step.

Summary updated

BibTeX
@inproceedings{tan2025can,
  author = {Jie Tan and Kangfei Zhao and Rui Li and Jeffrey Xu Yu and Chengzhi Piao and Hong Cheng and Helen Meng and Deli Zhao and Yu Rong},
  title = {{Can Large Language Models Be Query Optimizer for Relational Databases?}},
  booktitle = {Proc. ACM Manag. Data},
  year = {2025},
  url = {https://openreview.net/forum?id=vXrWj5OGN6},
}
Figure