When Is One Query per Template Enough? Adapting Learned Cost Models to New Hardware
Organizations: University of Waterloo, Waterloo, Canada
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.
Figures & tables
| noise, median Q | |||||||
|---|---|---|---|---|---|---|---|
| Env. | machine | CPUs | buffers | TPC-H | TPC-DS | SF10 | JOB |
| M1 | laptop | all 22 | 2 GB | 1.07 | 1.09 | 1.06 | |
| M2 | laptop | 1 | 2 GB | 1.07 | 1.08 | 1.03 | |
| M3 | server | all 64 | 2 GB | 1.05 | 1.05 | 1.05 | 1.05 |
| M4 | server | 1 | 2 GB | 1.07 | 1.03 | 1.07 | 1.04 |
| M5 | laptop | all 22 | 128 MB | 1.03 | 1.05 | ||
| Slowdown | Template share | of the log slowdown on | ||||||||
| Pair | Change | serial | parallel | shift | noise | parallel | size | both | features (new) | |
| TPC-H | ||||||||||
| M1 M2 | processors | 1.12 | 2.66 | 0.86 | 0.86 | 0.08 | 0.61 | 0.00 | 0.61 | 0.27 |
| M3 M4 | processors | 1.01 | 2.07 | 0.84 | 0.83 | 0.03 | 0.57 | 0.12 | 0.62 | 0.53 |
| M3 M6 | two processors | 1.00 | 1.32 | 0.69 | 0.68 | 0.03 | 0.51 | 0.06 | 0.55 | 0.31 |
| M1 M3 | hardware | 1.81 | 2.07 | 0.80 | 0.79 | 0.08 | 0.07 | 0.52 | 0.63 | 0.45 |
| Proc. (laptop) | Proc. (server) | Hardware | Buffer pool | |||||
|---|---|---|---|---|---|---|---|---|
| Operator | par. | ser. | par. | ser. | par. | ser. | par. | ser. |
| Seq Scan | 2.85 | 0.99 | 2.85 | 1.01 | 1.75 | 1.70 | 1.12 | 1.23 |
| Index Scan | 2.33 | 1.00 | 2.32 | 1.07 | 1.64 | 1.01 | 1.92 | 1.42 |
| Bitmap Heap Scan | 1.98 | 0.99 | 2.04 | 1.20 | 2.01 | 1.49 | 0.98 | 2.14 |
| Hash Join | 2.56 | 1.01 | 2.57 | 0.94 | 1.62 | 1.65 | 1.00 | 1.00 |
| Hash | 2.89 | 1.05 | 2.32 | 1.02 | 1.32 | 1.34 | 1.13 | 1.06 |
| , random | New templates, | |||||||||||
| Change | Pairs | Speed | Aff.+par. | Primary | Ridge | Primary | Primary | Oracle | Speed | Aff.+par. | Ridge | Oracle |
| TPC-H | ||||||||||||
| processors | 4 | 1.41 | 1.28 | 1.23 | 1.20 | 1.09 | 1.07 | 1.05 | 1.99 | 2.00 | 2.13 | 1.98 |
| two processors | 2 | 1.14 | 1.12 | 1.11 | 1.12 | 1.08 | 1.06 | 1.05 | 1.76 | 1.76 | 1.77 | 1.76 |
| hardware | 4 | 1.22 | 1.19 | 1.17 | 1.15 | 1.09 | 1.07 | 1.05 | 1.99 | 1.98 | 1.99 | 1.98 |
| both | 4 | 1.46 | 1.33 | 1.26 | 1.21 | 1.10 | 1.08 | 1.05 | 2.01 | 2.01 | 2.11 | 1.98 |
| Method | Selection | TPC-H | TPC-DS |
|---|---|---|---|
| No adaptation | 1.83 | 1.96 | |
| Speed factor | random | 1.37 | 1.45 |
| Ridge correction | random | 1.24 | 1.38 |
| Target only | random | 1.51 | 1.80 |
| Pooled refit | random | 1.36 | 1.36 |
| Strategy | Primary | 1-NN | Ridge | Seconds |
| TPC-H, | ||||
| random | 1.17 | 1.19 | 1.24 | 22 |
| round robin | 1.10 | 1.10 | 1.17 | 24 |
| cheapest per template | 1.17 | 1.17 | 1.23 | 16 |
| k-center | 1.13 | 1.13 | 1.21 | 21 |
| D-optimal | 1.14 | 1.13 | 1.20 | 16 |
| TPC-H | TPC-DS | |||||
| 10 | 25 | 100 | 10 | 25 | 100 | |
| Cost line plus forest | ||||||
| speed factor | 1.38 | 1.37 | 1.37 | 1.48 | 1.46 | 1.44 |
| template correction | 1.22 | 1.10 | 1.08 | 1.42 | 1.35 | 1.19 |
| Neural (DACE-style) | ||||||
| no adaptation | 1.84 | 1.84 | 1.84 | 1.89 | 1.89 | 1.89 |
| E1: SF10, 6 pairs | E2: JOB, 20 pairs | |||
|---|---|---|---|---|
| Method | ||||
| No adaptation | 1.66 | 1.66 | 2.71 | 2.71 |
| Speed factor | 1.31 | 1.30 | 2.51 | 2.48 |
| Affine+parallel | 1.31 | 1.26 | 2.55 | 2.42 |
| Ridge correction | 1.31 | 1.16 | 2.88 | 2.45 |
| Primary | 1.25 | 1.12 | 2.49 | 2.41 |
| Hypothesis and result | ||
|---|---|---|
| H1 | Template share above 0.5 in every pair (both directions of a pair share one value); smallest 0.74. | holds |
| H2 | : primary (round robin) beats the speed factor (random) in every pair and the ridge correction (random) in at least two thirds; 18 and 16 of 18. | holds |
| H3 | : affine+parallel (random) has a lower error than the speed factor (random) in at least three quarters of the pairs that change the processor count; 10 of 12. | holds |
| H4 | New templates: the speed factor’s error is at most 1.15 times the oracle’s in every pair; largest 1.08. | holds |
| H5 | : k-center with 1-NN has a lower error than random selection with the ridge correction in at least two thirds; 18 of 18. | holds |
| E1: TPC-H SF10 | E2: JOB | ||||
| Test | Count | Result | Count | Result | |
| H1 | template share , every pair | 6/6 | holds | 18/20 | fails |
| H2 | : primary below speed (interval), every pair; below ridge, | 5/6, 4/6 | fails | 2/20, 0/20 | fails |
| H3 | : aff.+par. below speed, of processor pairs | 2/6 | fails | 6/16 | fails |
| H4 | new templates: speed / oracle , every pair | 6/6 | holds | 20/20 | holds |
| H5 | : k-center 1-NN below ridge, | 6/6 | holds | 0/20 | fails |