Plan Stitch: Harnessing the Best of Many Plans

Bailu Ding, Sudipto Das, Wentao Wu, Surajit Chaudhuri, Vivek R. Narasayya

TL;DR

Database query performance can suffer when the query optimizer chooses a suboptimal execution plan. This paper introduces "Plan Stitch," a novel technique that automatically combines efficient sub-plans from previously executed queries to create a new, cheaper plan, significantly outperforming existing reversion-based correction methods.

What Problem Does This Paper Address?

In production database systems, query performance can unexpectedly degrade when the query optimizer selects a new execution plan that is significantly slower than previous ones. This "plan regression" is a common and frustrating issue. While current systems can revert to a previously known good plan, this approach is conservative and misses opportunities to leverage efficient parts of other plans. The challenge is to automatically and safely construct a new, superior plan by intelligently merging these efficient sub-plans without introducing new risks or excessive overhead.

Methods

The core of Plan Stitch is its ability to construct a "stitched plan" by combining sub-plans from a history of previously executed plans for the same query.

  • Constrained Search Space: Plan Stitch first identifies all unique physical operators and their logical expressions that have appeared in past executions. This forms a constrained search space, limiting the search to known, valid components.
  • AND-OR Graph: This constrained space is represented using an AND-OR graph, a structure that compactly encodes the relationships between operators and sub-plans.
  • Dynamic Programming: A dynamic programming algorithm is used to find the cheapest possible plan within this constrained search space. It works bottom-up, combining the costs of sub-plans to estimate the cost of larger sub-plans.
  • Cost Estimation: Crucially, Plan Stitch uses the observed execution costs of operators from past executions to estimate the cost of the stitched plan. This direct use of empirical data is key to its low-risk approach.
  • Plan Forcing: The generated stitched plan is then "forced" onto the query optimizer, ensuring it will be used for future executions. This also serves as a validation step.
  • Two-Stage Stitch: To handle potential validation failures, Plan Stitch employs a two-stage approach, including a "sparse stitch" that simplifies the search space if the initial attempt fails.

Datasets

  • TPC-DS benchmark: A standard benchmark for decision support systems.
  • Three real-world customer workloads: Used to evaluate Plan Stitch in practical scenarios.

Key Insights and Ideas

  • Leveraging Sub-optimal Plans: Even plans that are not the absolute best can contain highly efficient sub-components (e.g., specific join strategies or index accesses) that can be combined to create a superior overall plan.
  • Empirical Costing is Key: Directly using observed execution costs from past runs, rather than relying solely on theoretical cost models, significantly reduces the risk of plan regression.
  • Automated Combination is Feasible: The AND-OR graph and dynamic programming approach provide an efficient way to explore and construct these combined plans automatically.
  • Plan Stitch is Opportunistic: The technique doesn't require query regression to be beneficial; it can improve plans even when they are already good, by finding better combinations.

Key Results

  • Significant Cost Reduction: Plans generated by Plan Stitch can reduce execution costs by up to two orders of magnitude compared to simply reverting to the cheapest previously executed plan.
  • Broad Improvement: Plan Stitch improves the quality of up to 20x more plans than reversion-based correction (RBPC) across various workloads.
  • Low Risk of Regression: The risk of Plan Stitch creating a plan that is worse than RBPC is very low, with less than 2.7% of stitched plans regressing by more than 10%.
  • Accurate Cost Estimation: The estimated execution cost of stitched plans is within 20% of the true cost for at least 70% of cases.
  • Overhead: The overhead of Plan Stitch is less than the time taken by the optimizer to generate a plan from scratch.
  • Structural Changes: A significant portion (over 63%) of stitched plans exhibit structural differences from the original plans, highlighting the complexity of manual optimization.

Limitations and Future Work

  • Cost Misestimation with Data Changes: While generally accurate, cost estimation can degrade when data changes significantly, leading to increased error.
  • Optimizer's Search Strategy: In some cases, the optimizer might miss cheaper stitched plans due to its search strategy or cost misestimations, even when the stitched plan's estimated cost is lower.
  • Future Work: The authors suggest exploring alternative objectives for stitching (e.g., low variance of execution cost) and adapting Plan Stitch for specific parameterizations of queries. They also note that while their current approach is external to the DBMS, integrating it internally could offer further benefits.

Summary updated

BibTeX
@article{ding2018plan,
  author = {Bailu Ding and Sudipto Das and Wentao Wu and Surajit Chaudhuri and Vivek R. Narasayya},
  title = {{Plan Stitch: Harnessing the Best of Many Plans}},
  journal = {Proc. VLDB Endow.},
  year = {2018},
  doi = {10.14778/3231751.3231761},
}
Figure