Can Large Language Models Be Query Optimizer for Relational Databases?
Query optimization is a complex planning and decision-making problem within the exponentially growing plan space in database management systems (DBMS). Traditional optimization techniques have been extensively studied over decades, leaving limited room for further improvement along this track. Recent developments of Large Language Models (LLMs) have demonstrated their potential in solving complex planning and decision-making problems, such as arithmetic and programmatic tasks. In this paper, we try to explore the potential of LLMs in handling query optimization and propose a tentative LLM-based query optimizer dubbed LLM-QO, established on PostgreSQL's execution engine. In LLM-QO, we formulate query optimization in an autoregressive fashion which directly generates the execution plan without explicit plan enumeration. To investigate the essential input of LLM-QO, we design a customized data recipe named QInstruct to collect the training data from various optimizers and serialize the database's meta data, queries and corresponding plans into a textual format. Based on QInstruct, we implement a two-stage fine-tuning pipeline, Query Instruction Tuning (QIT) and Query Direct Preference Optimization (QDPO), to empower the capability of general-purpose LLMs in handling query optimization. In our experiments, LLM-QO can generate valid and high-quality plans and consistently outperforms both traditional and learned optimizers on three query workloads. Our findings verify that LLMs can be derived as query optimizers where generalization, efficiency and adaptivity deserve further research efforts.
Authors
- Rui Li (ORCID: https://orcid.org/0000-0002-3708-0257)
- Helen Meng (ORCID: https://orcid.org/0000-0002-4427-3532)
- Chengzhi Piao (ORCID: https://orcid.org/0009-0003-4942-0815)
- Jeffrey Xu Yu (ORCID: https://orcid.org/0000-0002-9738-827X)
- Yu Rong (ORCID: https://orcid.org/0000-0001-7387-302X)
- Deli Zhao (ORCID: https://orcid.org/0000-0002-8838-578X)
- Hong Cheng (ORCID: https://orcid.org/0000-0002-4673-2587)
- Kangfei Zhao (ORCID: https://orcid.org/0000-0002-7189-983X)
- Jie Tan (ORCID: https://orcid.org/0009-0001-5126-9101)
Institutions
- Hong Kong Baptist University (HK)
- Chinese University of Hong Kong (HK)
- Guangzhou University (CN)
- Alibaba Group (China) (CN)
- South China University of Technology (CN)
Publication Details
- Journal
- Proceedings of the ACM on Management of Data
- Published
- 2025-12-04
- DOI
- https://doi.org/10.1145/3769771
- Citations
- 1
- Primary Topic
- Advanced Database Systems and Queries
- Type
- article
- Field-Weighted Citation Impact
- 1.49