A Query Optimization Method Utilizing Large Language Models

Zhiming Yao, Haoyang Li, Jing Zhang, Cuiping Li, Hong ChenarXiv 2025

Abstract

Query optimization is a critical task in database systems, focused on determining the most efficient way to execute a query from an enormous set of possible strategies. Traditional approaches rely on heuristic search methods and cost predictions, but these often struggle with the complexity of the search space and inaccuracies in performance estimation, leading to suboptimal plan choices. This paper presents LLMOpt, a novel framework that leverages Large Language Models (LLMs) to address these challenges through two innovative components: (1) LLM for Plan Candidate Generation (LLMOpt(G)), which eliminates heuristic search by utilizing the reasoning abilities of LLMs to directly generate high-quality query plans, and (2) LLM for Plan Candidate Selection (LLMOpt(S)), a list-wise cost model that compares candidates globally to enhance selection accuracy. To adapt LLMs for query optimization, we propose fine-tuning pre-trained models using optimization data collected offline. Experimental results on the JOB, JOB-EXT, and Stack benchmarks show that LLMOpt(G) and LLMOpt(S) outperform state-of-the-art methods, including PostgreSQL, BAO, and HybridQO. Notably, LLMOpt(S) achieves the best practical performance, striking a balance between plan quality and inference efficiency.

Introduction: The Complexity of Query Optimization

Query optimization serves as the "brain" of a database management system (DBMS). When a SQL query is submitted, the optimizer must determine the most efficient execution strategy, involving decisions on join orders and data access methods. However, the search space for these plans is combinatorially explosive; for a query involving $n$ tables, there are $O(n!)$ possible execution plans.

The massive search space of query optimization, characterized by $O(n!)$ tree structures and various join/scan node types.

Traditional optimizers, such as the one in PostgreSQL, typically rely on heuristic search rules to prune this space and mathematical cost models to estimate execution times. These methods frequently fall short because heuristics can overlook optimal plans, and cost models often rely on simplified assumptions about data distribution, leading to significant performance degradation.

The LLMOpt Framework

To overcome these limitations, we propose LLMOpt, a framework that integrates fine-tuned Large Language Models (specifically Llama-3.1-8B) into the optimization pipeline. LLMOpt introduces two primary methodologies:

LLMOpt(G): Generative Candidate Generation

Instead of a step-by-step heuristic search, LLMOpt(G) utilizes the LLM as a generative optimizer. By processing the SQL query alongside detailed database statistics (e.g., cardinality, histograms), the model directly outputs serialized query plans as "hints." A sampling strategy is employed to generate a diverse set of 16 candidate plans, which are then ranked using a learned Tree-CNN cost model to identify the best option.

LLMOpt(S): List-wise Candidate Selection

LLMOpt(S) reimagines the selection process by replacing traditional point-wise cost models with a list-wise approach. Rather than estimating the absolute cost of plans in isolation, LLMOpt(S) evaluates a list of candidate plans simultaneously. This allows the LLM to leverage its global reasoning capabilities to rank plans relative to one another, which is inherently more accurate than predicting precise execution latencies.

The overall architecture of LLMOpt, showing the workflows for both candidate generation (LLMOpt(G)) and candidate selection (LLMOpt(S)).

To ensure the DBMS adheres to the LLM's suggested structures, we utilize a refined version of pg_hint_plan, which forces the database engine to follow the generated execution plan precisely.

Experimental Evaluation

The framework was evaluated using three industry-standard benchmarks: Join-Order-Benchmark (JOB), JOB-EXT, and Stack. The models were fine-tuned on a large offline dataset of query plans and their actual execution times, enabling the LLM to develop "database intuition" that generic models like GPT-4o lack.

Key Results and Insights

  • Significant Performance Gains: LLMOpt(S) achieved a 67.2% reduction in execution latency on the JOB benchmark compared to the default PostgreSQL optimizer.
  • Superior Selection Accuracy: LLMOpt(S) identified the optimal plan with 76.1% accuracy, significantly outperforming previous learned models such as Tree-CNN (31.8%) and Tree-LSTM (43.3%).
  • Practical Efficiency: While LLMOpt(G) produces high-quality plans, the token generation process introduces latency. LLMOpt(S), which only outputs the index of the best plan from a pre-defined set, offers the best balance of plan quality and inference speed for real-time applications.
  • Importance of Context: Including detailed database statistics in the prompt is critical. Without this context, the LLM cannot accurately reason about data distributions.

Conclusion and Future Work

LLMOpt demonstrates that fine-tuned LLMs can effectively replace or augment traditional database optimization components. While the generative approach (LLMOpt(G)) shows promise, the selection-based approach (LLMOpt(S)) is currently more practical for production environments due to its lower inference overhead. Future research will focus on accelerating inference through techniques like speculative decoding and expanding support for complex query structures such as nested subqueries.

Summary updated

BibTeX
@misc{yao2025query,
  author = {Zhiming Yao and Haoyang Li and Jing Zhang and Cuiping Li and Hong Chen},
  title = {{A Query Optimization Method Utilizing Large Language Models}},
  year = {2025},
  eprint = {2503.06902},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.06902},
}
Figure