When Is One Query per Template Enough? Adapting Learned Cost Models to New Hardware

Krish PatelarXiv 2026

TL;DR

Learned cost models for database queries break when you move to new hardware, but you don't need to retrain them from scratch. This paper shows that running just one query per template on the new machine and applying a per-template offset can adapt a model to within 3% of the accuracy of a natively trained one.

What Problem Does This Paper Address?

Modern database systems increasingly use "learned cost models"—machine learning models that predict how long a query will run based on its execution plan. These models are significantly more accurate than traditional rule-based optimizers, but they have a major weakness: they are hardware-dependent.

If you train a model on a laptop and then move your database to a high-end cloud server, the runtimes change. While you might expect a simple "speed factor" (e.g., "everything is 2x faster") to work, it rarely does. Different queries react differently to hardware changes—for instance, a parallel scan might speed up significantly with more cores, while a serial join might not. The challenge is finding a way to adapt these models to new hardware without the massive expense of re-running thousands of queries to generate a new training set.

Methods

The author, Krish Patel, investigates how to adapt a "moved" model using a small budget of $k$ measured queries on the new machine. He compares several strategies:

  1. Baselines: Simple global rescaling (Speed Factor) and retraining from scratch on the $k$ queries.
  2. Template Correction (Primary Method): This method fits a simple linear model to account for global shifts (specifically distinguishing between serial and parallel plans) and then adds a specific "offset" for every query template measured.
  3. Neural Fine-tuning: Taking a state-of-the-art neural cost model (DACE) and fine-tuning its weights on the new $k$ measurements.
  4. Query Selection: Instead of picking $k$ queries randomly, the paper explores "Round Robin" selection (one query from each template) and active learning techniques like k-center and D-optimal design.

Datasets

The study utilizes a robust set of benchmarks across six different hardware environments (varying CPU counts, pinning, and buffer pool sizes):

  • TPC-H & TPC-DS (SF1): Standard decision-support benchmarks. The author contributes a new dataset of 1,162 identical plans timed across all six environments.
  • TPC-H (SF10): A larger scale factor to test if findings hold as data grows.
  • Join Order Benchmark (JOB): Uses real-world IMDB data. Unlike TPC templates, JOB queries within a template can vary significantly in runtime, providing a harder test case.

Key Insights and Ideas

  • The "Template Share" of Variance: A crucial finding is that most of the variance in how runtimes shift (slowdown) happens between templates, not within them. As shown in the paper's analysis, between 69% and 94% of the log slowdown variance is template-dependent.
  • Parallelism is a Primary Driver: When changing processor counts, the single most important feature for predicting slowdown is whether a plan uses parallel workers.
  • One Query per Template is a "Sweet Spot": Once you have measured one query for a template, measuring more from that same template yields diminishing returns.
  • Simple Beats Complex: Surprisingly, the simple "Template Correction" method on a forest-based model often performs as well as, or better than, fine-tuning complex neural networks, especially when the measurement budget is low.

Key Results

The paper demonstrates that with a budget of one query per template (round-robin selection), the adapted model achieves impressive accuracy:

  • Accuracy: On TPC-H and TPC-DS, the primary method reached a median Q-error of 1.10 and 1.19 respectively. This is remarkably close to the "Oracle" (a model trained on the entire target dataset).
  • Efficiency: As seen in Figure 1, the Template Correction method (dashed purple line) drops in error much faster than simple rescaling (blue line) as more queries are measured.
Error vs. number of measured queries (k) for different adaptation methods.
  • Neural Comparison: Figure 2 shows that while neural fine-tuning is effective, the simpler template correction on the neural model's predictions is often just as good, suggesting the "shift" is fundamentally simple to model once you account for templates.
Fig. 2. Error against k with round-robin selection, geometric mean over the 30 development pairs of each workload. Blue: the speed factor on the forest model (the cost line plus forest); solid purple: the template correction on the forest model; dashed purple: the same correction on the neural model; orange: the neural model fine-tuned on the same queries, all weights (solid) or the head only (dotted). The black dotted line is the neural model retrained on the whole target pool.

Limitations and Future Work

  • New Templates: The biggest limitation is that adaptation doesn't help with unseen templates. If a query comes from a template that wasn't in the original training set or the $k$ adaptation queries, the model's error remains high.
  • Fixed Plans: The study assumes the execution plans remain identical across hardware. In practice, a database optimizer might choose a different plan on a machine with more memory or faster I/O.
  • Memory Constraints: The datasets used mostly fit in memory. The author notes that adapting models for "larger-than-memory" data, where disk I/O becomes the bottleneck, is a necessary next step.
  • Database Systems: The evaluation was limited to PostgreSQL; testing across other engines (commercial or specialized OLAP) would further validate the findings.

Summary updated

Abstract

A learned cost model that predicts query runtimes is trained on measurements from one machine. When the database moves to other hardware, its plans stay the same but their runtimes change, each by its own factor. We ask how to adapt such a model from k queries measured on the new machine: which queries to run, how to use their runtimes, and how many are enough. On PostgreSQL we timed the same plans of 1,162 TPC-H and TPC-DS queries in six environments: a laptop and a server, each with all its processors or pinned to one, the server also pinned to two, and the laptop also with a small buffer pool. No single factor describes the slowdown between two environments: most of its variance lies between query templates, so a speed factor stops improving after a few queries. A correction that fits separate lines for serial and parallel plans and adds an offset for every measured template brings the moved model, with one measured query per template, to an error (geometric mean over templates of the median Q-error) of 1.10 on TPC-H and 1.19 on TPC-DS, close to the same correction on a model trained on the new machine; fine-tuning a DACE-style neural cost model on the same queries gains at most 2%. On templates never measured, no correction we tried is on average more than 2% better than the speed factor. We then fixed hypotheses and timed new data. On TPC-H at ten times the scale the errors follow these results, but two of five repeated tests miss. On the Join Order Benchmark, whose queries differ widely within a template, the model errs by 2.36 even when retrained on the new machine, no correction gains much, and only the test on new templates holds; boosted trees and a neural model fit these queries better, and correcting the neural model works as well as fine-tuning it. We report every test as registered.

Empirical ResultsBrowse all benchmarks →

BenchmarkMethodMetricScoreProtocol / Evidence
TPC-HTemplate correction (primary method)Error (geometric mean of median Q-error over test templates)1.10Query Processing · split: test-templates · one-measured-query-per-template
Source quote
L988: method reaches 1.10 on TPC-H at k = 25 and 1.19 on TPC-
TPC-DSTemplate correction (primary method)Error (geometric mean of median Q-error over test templates)1.19Query Optimization · split: test-templates · one-measured-query-per-template
Source quote
L988: method reaches 1.10 on TPC-H at k = 25 and 1.19 on TPC-
Reported baseline / comparator measurements (1)
BenchmarkMethodMetricScoreProtocol / Evidence
Join Order Benchmark (JOB)Model retrained on the new machine baselineError (geometric mean of median Q-error over templates)2.36retrained-on-the-new-machine
Source quote
L32: template, the model errs by 2.36 even when retrained on the
BibTeX
@misc{patel2026when,
  author = {Krish Patel},
  title = {{When Is One Query per Template Enough? Adapting Learned Cost Models to New Hardware}},
  year = {2026},
  eprint = {2611.05892},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.05892},
}
Figure