Improve CostHashAgg with single-column NDV and spill-aware cost model#1672
Draft
yjhjstz wants to merge 1 commit intoapache:mainfrom
Draft
Improve CostHashAgg with single-column NDV and spill-aware cost model#1672yjhjstz wants to merge 1 commit intoapache:mainfrom
yjhjstz wants to merge 1 commit intoapache:mainfrom
Conversation
Two enhancements to CostHashAgg in CCostModelGPDB: 1. Single-column NDV optimization for local partial HashAgg: When GROUP BY has exactly 1 column, use GetNDVs() (global NDV from column statistics) instead of pci->Rows() to estimate the output row count of the local partial aggregation stage. GetNDVs() returns the global NDV directly, so no * UlHosts() scaling is needed. This lets the optimizer distinguish high-NDV cases (partial agg streams nearly as many rows as input, 2-phase has little benefit) from low-NDV cases (partial agg significantly reduces data before redistribution, 2-phase is preferred). Multi-column GROUP BY falls back to the original behavior: num_output_rows = pci->Rows() * UlHosts(). 2. Spill-aware cost model: When num_output_rows * width exceeds the spilling memory threshold (EcpHJSpillingMemThreshold, 50 MB), apply higher cost unit values to reflect disk I/O overhead. Uses the existing HJ spilling cost parameters (EcpHJFeedingTupColumnSpillingCostUnit etc.) which are already tuned for spilling scenarios. TPC-H benchmark: -14.3% overall (Q17 -60%, Q03 -6%). TPC-DS benchmark: -0.4% overall (Q59 -29%).
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.
Changes
Two enhancements to
CostHashAggin GPORCA'sCCostModelGPDB:Single-column NDV optimization: When
GROUP BYhas exactly one column, useGetNDVs()(global NDV from column statistics) instead ofpci->Rows()to estimate the local partial aggregation output. This allows the optimizer to distinguish high-NDV cases (partial agg streams nearly as many rows as input → 1-phase preferred) from low-NDV cases (partial agg significantly reduces data → 2-phase preferred). Multi-columnGROUP BYfalls back to the originalpci->Rows() * UlHosts()logic.Spill-aware cost model: When
num_output_rows × widthexceeds the 50 MB spilling threshold, apply higher cost unit values to reflect disk I/O overhead.TPC-H: -13.0% overall (274,201ms → 238,438ms)
Only 2 queries changed plans:
Q17 is the dominant win.
l_partkeyis a high-NDV single-column group key (~6.6M distinct values); partial aggregation produced 19.6M output rows (nearly identical to input), causing 1.3 GB of disk spill in the Finalize stage. The optimizer now correctly chooses 1-phase, reducing spill to 490 MB and cutting latency by 36 seconds. No plan change caused a regression.TPC-DS: +0.1% overall (605,646ms → 606,421ms)
Only 2 queries changed plans:
Combined Assessment
Fixes #ISSUE_Number
What does this PR do?
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions