Repository navigation
fix(plan): correct prepared range index costing - #26974
Conversation
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep review completed for exact head fe2332f994f73d14d7e83f9b6ebc5ac6f6406b24.
No blocking findings.
I traced candidate discovery and costing through both index-only and backfill materialization. The change keeps prepared-range point estimates only for sibling ranking, uses parameter-independent lower work versus a full base-scan upper bound for absolute rejection, preserves known work in mixed OR branches, and does not credit residual selectivity unless the candidate can evaluate it before backfill. Block-granular base work, FORCE/small-table compatibility, invalid-stat fallback, scratch-state reuse, NULL-sensitive residual rechecks, and LIMIT safety were also checked.
Validation performed on this exact head:
git diff --check- focused prepared-range/index-cost regressions
- full
go test ./pkg/sql/plan go vet ./pkg/sql/plan- head/base
BenchmarkEncodedIndexCostWideTableMultiIndexcomparison: both 48 allocs/op and 13,592 B/op, with overlapping timing distributions
CI at submission: 11 passed, 0 failed, 6 still pending; PR is still Draft.
Code-review verdict: LGTM. GitHub does not allow the current account to APPROVE its own PR, so this is recorded as a COMMENT review instead.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
## Summary - keep prepared range index candidates when unknown runtime bounds make base-vs-index cost intervals overlap - use expected work only for ranking sibling index candidates, and require an interval-dominant lower bound before rejecting every index - preserve candidate-specific backfill work when an unknown residual cannot be evaluated by the index - align the base-scan control with the planner's block-granular row estimate ## Root cause PR matrixorigin#26850 introduced a common encoded-index cost model, but the same point estimate was used both to rank index candidates and to reject all indexes in favor of a base scan. For prepared range predicates, bound values are unavailable at planning time, so the point estimate is not strong enough for that absolute decision. The base control could also use a sub-block row estimate while the index side charged physical scan work. The fix keeps the point estimate for relative candidate ranking, then compares uncertainty intervals for absolute rejection. Unknown leading ranges can make candidate work empty; unknown residuals reduce pre-backfill work only when that candidate can actually evaluate them. Fixes matrixorigin#26971. ## Impact The `sysbench_random_ranges` prepared query returns to the secondary index instead of a base table scan. On the local 2M-row reproduction: - before: about 987 TPS, P95 1.87s - after: about 16,779 TPS, P95 103ms - final single-query validation: `Index Table Scan on sbtest1.k_1`, 8 blocks, 60 input rows, about 6ms The original matrixorigin#26850 behavior remains covered: broad covering/backfill candidates are rejected, selective candidates are retained, and sibling selection remains independent of DDL order. ## Validation - `GOWORK=off go list -mod=readonly ./pkg/sql/plan` - `GOWORK=off go build -mod=readonly ./pkg/sql/plan` - `GOWORK=off go vet -mod=readonly ./pkg/sql/plan` - 33 focused encoded-index planner tests - full `pkg/sql/plan` test suite - `BenchmarkEncodedIndexCostWideTableMultiIndex`, 5 runs: 20.27-20.43 us/op, 13,592 B/op, 48 allocs/op - isolated public-plan counterexample: removing candidate-stage uncertainty incorrectly selects `idx_state_time_pk`; the current implementation correctly keeps the base scan
## Summary - keep prepared range index candidates when unknown runtime bounds make base-vs-index cost intervals overlap - use expected work only for ranking sibling index candidates, and require an interval-dominant lower bound before rejecting every index - preserve candidate-specific backfill work when an unknown residual cannot be evaluated by the index - align the base-scan control with the planner's block-granular row estimate ## Root cause PR matrixorigin#26850 introduced a common encoded-index cost model, but the same point estimate was used both to rank index candidates and to reject all indexes in favor of a base scan. For prepared range predicates, bound values are unavailable at planning time, so the point estimate is not strong enough for that absolute decision. The base control could also use a sub-block row estimate while the index side charged physical scan work. The fix keeps the point estimate for relative candidate ranking, then compares uncertainty intervals for absolute rejection. Unknown leading ranges can make candidate work empty; unknown residuals reduce pre-backfill work only when that candidate can actually evaluate them. Fixes matrixorigin#26971. ## Impact The `sysbench_random_ranges` prepared query returns to the secondary index instead of a base table scan. On the local 2M-row reproduction: - before: about 987 TPS, P95 1.87s - after: about 16,779 TPS, P95 103ms - final single-query validation: `Index Table Scan on sbtest1.k_1`, 8 blocks, 60 input rows, about 6ms The original matrixorigin#26850 behavior remains covered: broad covering/backfill candidates are rejected, selective candidates are retained, and sibling selection remains independent of DDL order. ## Validation - `GOWORK=off go list -mod=readonly ./pkg/sql/plan` - `GOWORK=off go build -mod=readonly ./pkg/sql/plan` - `GOWORK=off go vet -mod=readonly ./pkg/sql/plan` - 33 focused encoded-index planner tests - full `pkg/sql/plan` test suite - `BenchmarkEncodedIndexCostWideTableMultiIndex`, 5 runs: 20.27-20.43 us/op, 13,592 B/op, 48 allocs/op - isolated public-plan counterexample: removing candidate-stage uncertainty incorrectly selects `idx_state_time_pk`; the current implementation correctly keeps the base scan
## Summary - keep prepared range index candidates when unknown runtime bounds make base-vs-index cost intervals overlap - use expected work only for ranking sibling index candidates, and require an interval-dominant lower bound before rejecting every index - preserve candidate-specific backfill work when an unknown residual cannot be evaluated by the index - align the base-scan control with the planner's block-granular row estimate ## Root cause PR matrixorigin#26850 introduced a common encoded-index cost model, but the same point estimate was used both to rank index candidates and to reject all indexes in favor of a base scan. For prepared range predicates, bound values are unavailable at planning time, so the point estimate is not strong enough for that absolute decision. The base control could also use a sub-block row estimate while the index side charged physical scan work. The fix keeps the point estimate for relative candidate ranking, then compares uncertainty intervals for absolute rejection. Unknown leading ranges can make candidate work empty; unknown residuals reduce pre-backfill work only when that candidate can actually evaluate them. Fixes matrixorigin#26971. ## Impact The `sysbench_random_ranges` prepared query returns to the secondary index instead of a base table scan. On the local 2M-row reproduction: - before: about 987 TPS, P95 1.87s - after: about 16,779 TPS, P95 103ms - final single-query validation: `Index Table Scan on sbtest1.k_1`, 8 blocks, 60 input rows, about 6ms The original matrixorigin#26850 behavior remains covered: broad covering/backfill candidates are rejected, selective candidates are retained, and sibling selection remains independent of DDL order. ## Validation - `GOWORK=off go list -mod=readonly ./pkg/sql/plan` - `GOWORK=off go build -mod=readonly ./pkg/sql/plan` - `GOWORK=off go vet -mod=readonly ./pkg/sql/plan` - 33 focused encoded-index planner tests - full `pkg/sql/plan` test suite - `BenchmarkEncodedIndexCostWideTableMultiIndex`, 5 runs: 20.27-20.43 us/op, 13,592 B/op, 48 allocs/op - isolated public-plan counterexample: removing candidate-stage uncertainty incorrectly selects `idx_state_time_pk`; the current implementation correctly keeps the base scan
Summary
Root cause
PR #26850 introduced a common encoded-index cost model, but the same point estimate was used both to rank index candidates and to reject all indexes in favor of a base scan. For prepared range predicates, bound values are unavailable at planning time, so the point estimate is not strong enough for that absolute decision. The base control could also use a sub-block row estimate while the index side charged physical scan work.
The fix keeps the point estimate for relative candidate ranking, then compares uncertainty intervals for absolute rejection. Unknown leading ranges can make candidate work empty; unknown residuals reduce pre-backfill work only when that candidate can actually evaluate them.
Fixes #26971.
Impact
The
sysbench_random_rangesprepared query returns to the secondary index instead of a base table scan. On the local 2M-row reproduction:Index Table Scan on sbtest1.k_1, 8 blocks, 60 input rows, about 6msThe original #26850 behavior remains covered: broad covering/backfill candidates are rejected, selective candidates are retained, and sibling selection remains independent of DDL order.
Validation
GOWORK=off go list -mod=readonly ./pkg/sql/planGOWORK=off go build -mod=readonly ./pkg/sql/planGOWORK=off go vet -mod=readonly ./pkg/sql/planpkg/sql/plantest suiteBenchmarkEncodedIndexCostWideTableMultiIndex, 5 runs: 20.27-20.43 us/op, 13,592 B/op, 48 allocs/opidx_state_time_pk; the current implementation correctly keeps the base scan