How I Learned to Stop Worrying and Love Re-optimization

Matthew Perron, Zeyuan Shang, Tim Kraska, Michael Stonebraker

Abstract

While modern database optimizers handle most queries well, a "long tail" of complex queries remains orders of magnitude slower than necessary due to poor cardinality estimation. This paper demonstrates that instead of chasing the "impossible dream" of perfect estimation, a simple re-optimization strategy—detecting errors at runtime and re-planning—can recover most of the performance loss. Our evaluation on the Join Order Benchmark (JOB) shows that re-optimization improves the longest-running queries by 27% and reduces total workload execution time by 45%.

Introduction

Query optimizers are the "brains" of a database, responsible for finding the most efficient way to execute a SQL query. Since the 1970s, they have relied on mathematical models to estimate cardinality—the number of rows a join will produce. However, these models typically rely on two flawed assumptions: independence (assuming data in different columns are unrelated) and uniformity (assuming data is spread evenly).

When these assumptions fail, the optimizer often chooses a disastrously slow plan. While researchers have explored complex statistics or machine learning to improve estimation, these solutions are often difficult to implement or fail to change the final execution plan for the better.

Methodology

The authors investigate the limits of optimization through two primary approaches:

  1. The "Perfect-(n)" Oracle: By modifying PostgreSQL to accept "perfect" cardinality estimates for joins involving up to $n$ tables, the authors measured the degree of accuracy required to significantly improve a query plan.
  2. Simulated Re-optimization: A "materialize-and-replan" scheme was implemented. During execution, if the actual row count differs from the estimate by a factor greater than a specific threshold (the Q-error), execution stops. Intermediate results are saved to a temporary table, and the optimizer generates a new plan based on this perfect runtime information.

The study utilizes the Join Order Benchmark (JOB), based on the real-world IMDB dataset. Unlike synthetic benchmarks, JOB contains the correlations and data skew that challenge modern optimizers across its 113 queries.

The Perfect-(4) Threshold

A key finding of this research is that incremental improvements in cardinality estimation often yield no performance benefit. As shown in the figure below, providing perfect estimates for joins of 2 or 3 tables provides almost zero benefit to total workload time.

Execution and planning time for varying levels of perfect cardinality estimation (Perfect-n).

Significant speedups only occur once perfect estimates are provided for joins of 4 or more tables. This "Perfect-(4)" threshold explains why many research improvements fail to translate into real-world performance gains: they simply don't reach the level of accuracy needed to tip the optimizer toward a better plan.

Performance Results

Re-optimization serves as a safety net, treating the initial plan as a hypothesis and course-correcting based on runtime feedback. The authors found that a Q-error threshold of 32 provided the optimal balance between performance gains and re-planning overhead.

Performance comparison between standard PostgreSQL, partial perfect estimation, re-optimization, and full perfect estimation.

As illustrated in the comparison, the re-optimization strategy significantly closes the gap between standard PostgreSQL and a theoretical "perfect" estimator. For the 20 longest-running queries, latency improved by 27%. Across the entire JOB benchmark, total execution time was reduced by 45%. While re-optimization increases planning time by approximately 42%, this cost is negligible for long-running queries where execution takes minutes while planning takes milliseconds.

Discussion and Limitations

Despite its success, re-optimization faces several challenges. The "materialize-and-replan" approach requires saving intermediate results to disk, which can be expensive and breaks "pipelining" in streaming-optimized systems. Furthermore, for very short queries, the overhead of checking cardinalities and re-planning may lead to regressions. Future work may involve more sophisticated in-memory adaptive techniques or targeting re-optimization specifically for queries predicted to have high execution times.

Summary updated

BibTeX
@inproceedings{perron2019how,
  author = {Matthew Perron and Zeyuan Shang and Tim Kraska and Michael Stonebraker},
  title = {{How I Learned to Stop Worrying and Love Re-optimization}},
  booktitle = {ICDE},
  year = {2019},
  doi = {10.1109/icde.2019.00191},
}
Figure