Training-Free Query Optimization via LLM-Based Plan Similarity
Nikita Vasilenko, Alexander Demin, Vladimir Boorlakov · 2025
Read Training-Free Query Optimization via LLM-Based Plan Similarity by Nikita Vasilenko, Alexander Demin, Vladimir Boorlakov (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2506-05853.txt.
Introduction
Nikita Vasilenko and co-authors speed up database queries by asking a language model which past query resembles yours, in a 2025 paper. Databases pick an execution plan for each query, and the default plan is often far from best. Their framework, LLM-PM, turns the default plan into an embedding, a point in meaning space. It then looks up the nearest previously run plans.
The neighbors vote on which optimizer hints to apply. A consistency check guards the choice, with a full hint search as fallback. On a standard benchmark running on OpenGauss, query latency fell by about a fifth on average. No model training was needed at any point. The result hints at a cheap, practical role for language models in database internals.
Catalog record
| CREATOR | Nikita Vasilenko, Alexander Demin, Vladimir Boorlakov |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 41 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2506-05853
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
Training-Free Query Optimization via LLM-Based Plan Similarity Nikita Vasilenko, Alexander Demin, Vladimir Boorlakov Abstract Large language model (LLM) embeddings offer a promising new avenue for database query optimization. In this paper, we explore how pre-trained execution plan embeddings can guide SQL query execution without the need for additional model training. We introduce LLM-PM (LLM-based Plan Mapping), a framework that embeds the default execution plan of a query, finds its k nearest neighbors among previously executed plans, and recommends database hintsets based on neighborhood voting. A lightweight consistency check validates the selected hint, while a fallback mechanism searches the full hint space when needed. Evaluated on the JOB-CEB benchmark using OpenGauss, LLM-PM achieves an average speed-up of 21% query latency reduction. This work highlights the potential of LLM-powered embeddings to deliver practical improvements in query performance and opens new directions for training-free, embedding-based optimizer guidance systems. keywords Query Optimization, LLM for Databases, Database Hints 1 Introduction Query optimization is a core problem in relational database systems: the optimizer must construct an efficient execution plan for each SQL query from a vast search space of alternatives (potential join orders, join algorithms, table access methods, etc.). Decades of research have produced sophisticated cost-based optimizers, yet they remain imperfect and can still choose suboptimal plans that dramatically slow down query execution. A primary cause are inaccurate cardinality estimation and cost model which often relies on simplistic assumptions and thus can be wildly wrong. These mistakes lead to poor plan choices (e.g., using a nested-loop join where a hash join would be far faster). Modern database systems offer optimizer hints as a mechanism to influence or override plan choices. Hints are directives embedded in the SQL that guide the optimizer to consider specific strategies, such as using a particular join order, favoring a hash join over other join types, or using an index on a given table. In principle, a skilled DBA can use hints to steer the optimizer away from known bad decisions and toward a better plan. For example, a hint could force the use of a previously ignored index, or constrain the join order to avoid inefficient joins. The challenge, however, is that selecting the right hints is extremely complex – it requires a deep understanding of the query, the data, and the optimizer’s internal heuristics. An incorrect hint may even backfire and degrade performance rather than improve it. These difficulties have sparked interest in automating query optimization with machine learning. A recent trend is to treat the optimizer as a black box and use learning methods to assist it by suggesting hints or plan tweaks that lead to faster executions. This avoids wholesale replacement of the optimizer, focusing instead on guiding it toward better decisions. Recent studies have shown that Large Language Models (LLMs) can significantly enhance database query optimization. These models are increasingly employed in various ways, such as generating execution plans, embedding queries to understand their semantics, and providing optimization hints. The promising results from these approaches indicate that LLMs are becoming a valuable tool in the field. Given this progress, we believe that further exploration into LLM-driven query optimization is a worthwhile endeavor, with the potential to advance the efficiency and adaptability of database systems. However, despite these encouraging advances, LLM-based techniques remain far from being practical for real-world deployment due to the high cost and latency of inference, which limits their applicability in time-sensitive or resource-constrained environments. In this paper, we introduce a straightforward method that leverages Large Language Model (LLM) embeddings of execution plans—not the queries themselves—to identify optimal sets of optimizer hints. Our approach combines the representational power of LLM embeddings with a simple, fast, analogy-based prediction system that requires no training.We aim to bridge the gap between the expressive power of LLMs and the need for rapid, efficient query optimization systems. Our method effectively learns from past experiences without requiring an extensive training phase—the heavy lifting is done by the pre-trained LLM, which provides a semantic representation of execution plans. To validate this approach, we built a prototype on the openGauss database system and evaluated it on one standard benchmark: JOB-CEB (3133 query instances derived from 16 query templates on the IMDB dataset). In our experiments, the LLM-based method successfully guided the optimizer toward faster plans for a large portion of the queries. The results demonstrate that using LLM embeddings for plan hinting can consistently outperform the default optimizer, even without relying on complex training or modifications to the underlying system. In summary, this paper makes the following contributions: (1) We introduce a method for query optimization that uses LLM-generated execution plan embeddings to enhance optimizer hint selection. (2) We propose a lightweight, fast optimization approach that leverages pre-trained LLMs to guide the database optimizer toward better execution plans without requiring extensive training or system modifications. (3) We demonstrate the effectiveness of LLM-PM through experiments on standard benchmark (JOB-CEB), showing that LLM-based plan hinting can outperform the default optimizer. (4) We highlight the potential of LLM embeddings to improve query performance, offering a promising direction for efficient, LLM-driven query optimization systems. 2 Related Work Recent research into improving the quality of query optimization can be broadly divided into two main directions. The first is aimed at completely replacing the traditional cost-based optimizer with learned models that generate query execution plans from scratch. The second, more incremental direction seeks to augment or guide the existing optimizer using machine learning techniques by refining cost estimates, improving cardinality predictions, or introducing optimizer hints. Optimizers that Replace the Traditional Optimizer A first research line seeks to replace the entire cost–based optimizer with a learned model that decides the execution plan end‑to‑end. Neo Marcus et al. 2019 pioneered this direction: a tree‑convolution network predicts the latency of a candidate plan and a search component selects the plan with the lowest predicted latency. This approach demonstrated the feasibility of end-to-end learned planning but required a long training time and extensive labeled data. Deep‑learning cardinality models can also partially replace core optimizer logic: the MSCN model in Learned cardinalities: Estimating correlated joins with deep learning Kipf et al. 2019 and its follow‑up Cardinality Estimation with Local Deep Learning Models Ortiz et al. 2019 learn complex data correlations that traditional histograms miss, yielding better plans when integrated. These works demonstrate that partial or even full replacement of an optimizer is technically feasible, yet they come with significant trade‑offs: (i) they require very large training sets; (ii) they discard decades of handcrafted heuristics embodied in mature optimizers; and (iii) they raise open questions about how well the learned models generalize to unseen data distributions, schemas, and query workloads. Optimizers that Guide the Traditional Optimizer A second, more pragmatic line keeps the native optimizer but steers it with learned signals such as cost and cardinality corrections or optimizer hints. AQO Ivanov and Bartunov 2017 is a PostgreSQL extension that rewrites only the cardinality-estimation component of the native optimizer. After each query execution, AQO stores the observed cardinalities for every plan node in a per-query “memory” and trains a k -nearest-neighbour regressor in the feature space. When the same query template re-appears, the learned estimates are injected in place of the built-in formulas, letting the