Repository navigation
perf: share one StatisticsContext across all physical optimizer rules - #26094
Draft
asolimando wants to merge 1 commit into
Draft
asolimando wants to merge 1 commit into
asolimando wants to merge 1 commit into
Conversation
Create one StatisticsContext per optimize_physical_plan call and expose it through the new PhysicalOptimizerContext::statistics_context, so statistics computed by one rule are reused by later rules. JoinSelection, EnsureRequirements, AggregateStatistics and LimitPushdown use it. StatisticsContext stores its cache in a parking_lot::Mutex so it is Send + Sync, as PhysicalOptimizerContext requires.
Member
Author
|
@kosiew @zhuqi-lucas, could you run I figure you'd be interested in this PR as it builds on #25929, and it's the query-lifetime follow-up of what #25098 did for EnsureDistribution. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #26094 +/- ##
==========================================
- Coverage 82.72% 82.72% -0.01%
==========================================
Files 1147 1147
Lines 448179 448289 +110
Branches 448179 448289 +110
==========================================
+ Hits 370754 370828 +74
- Misses 54895 54924 +29
- Partials 22530 22537 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
Each physical optimizer rule that reads statistics creates its own
StatisticsContext, so the statistics of the same plan nodes are computed again in every rule.JoinSelectioncreates a new context for everyget_statscall, so it has no caching at all, even within its own pass.#25098 showed the gain of sharing one context inside
EnsureDistribution. #25929 made cache entries keep the plan node they were computed for, so a context can now be shared safely across plan rewrites. This PR uses that to share oneStatisticsContextacross all the rules of oneoptimize_physical_plancall.On TPC-DS (98 queries) and TPC-H (21 queries), sf1 Parquet, planned with a local harness (not part of this PR):
mainThe largest gain is TPC-DS q64: cache misses go from 3,785 to 531 and planning time drops by about 21%.
What changes are included in this PR?
StatisticsContextstores its cache in aparking_lot::Mutexinstead ofRc<RefCell>, so it isSend + Sync. This is needed becausePhysicalOptimizerContext: Send + Sync. The lock is held only for single map lookups and inserts, never across the recursive walk. Thecompute_statisticsbenchmark shows no difference frommain(all cases within ±4%, in both directions).PhysicalOptimizerContext::statistics_context(), which returnsNoneby default.DefaultPhysicalPlannercreates one context peroptimize_physical_plancall, built from the session's statistics registry, and returns it to every rule.ConfigOnlyContextalso owns aStatisticsContext, so a rule called throughoptimize()shares one cache for its whole pass.JoinSelection,EnsureRequirements(includingPlanSize::from_plan),AggregateStatisticsandLimitPushdownuse the shared context. When a context does not share one (for example, a context received through FFI), the rules create a new context from the statistics registry, as they did before.pushdown_limit_helperkeeps its signature and calls the newpushdown_limit_helper_with_stats, which takes a&StatisticsContext. This is the same pattern asensure_distribution_with_stats.What is the testing strategy for this PR?
optimizer_rules_share_statistics_contexttest inphysical_planner.rs: two rules compute the root statistics through the shared context, and the second one gets theArccached by the first.Are there any user-facing changes?
No breaking changes.
PhysicalOptimizerContext::statistics_context().pushdown_limit_helper_with_stats.StatisticsContextis nowSend + Sync.AggregateStatistics,LimitPushdownandPlanSize::from_plannow consult the session's statistics providers, asJoinSelectionandEnsureRequirementsalready do. Without registered providers (the default), their results are unchanged.Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding.