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:
- Baselines: Simple global rescaling (Speed Factor) and retraining from scratch on the $k$ queries.
- 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.
- Neural Fine-tuning: Taking a state-of-the-art neural cost model (DACE) and fine-tuning its weights on the new $k$ measurements.
- 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.
- 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.
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