Skip to content

fix(plan): correct prepared range index costing - #26974

Merged
XuPeng-SH merged 3 commits into
mainfrom
agent/fix-prepared-range-index-cost
Aug 11, 2026
Merged

XuPeng-SH merged 3 commits into
mainfrom
agent/fix-prepared-range-index-cost

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

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 #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_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 #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

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 BenchmarkEncodedIndexCostWideTableMultiIndex comparison: 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.

@XuPeng-SH
XuPeng-SH marked this pull request as ready for review August 11, 2026 20:56
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH
XuPeng-SH merged commit 3f2ed9b into main Aug 11, 2026
32 checks passed
@XuPeng-SH
XuPeng-SH deleted the agent/fix-prepared-range-index-cost branch August 11, 2026 21:46
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: sysbench random_ranges_1000 TPS 暴跌 97%,二分定位到 PR #26850 (cost encoded regular index access)

2 participants