Skip to content

fix(plan): relax index-only scan selectivity guard to fix random_ranges regression - #24338

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
jiangxinmeng1:fix-random-ranges-regression
May 11, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
jiangxinmeng1:fix-random-ranges-regression

Conversation

@jiangxinmeng1

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Bug fix

Which issue(s) does this PR fix?

Fixes #24335

What this PR does / why we need it:

PR #24309 added a selectivity guard in tryIndexOnlyScan to prevent OOM from non-selective secondary index range scans. However, the guard used InFilterCardLimitNonPK (10000) as the outcnt threshold, which is too aggressive for index-only scans.

For sysbench random_ranges (10 random BETWEEN ranges on column k with a secondary index, 10M row table):

  • The query only reads the indexed column → ideal for index-only scan
  • Estimated outcnt ≈ 10000 (10 ranges × ~1000 rows each)
  • The guard rejects the index-only scan, forcing a full table scan → 45% TPS regression

Index-only scans don't access the base table, so the OOM scenario (excessive metadata/bloom filter re-reads from base table join) doesn't apply. The fix uses GetInFilterCardLimitOnPK (scales with table size, up to 1M) instead of the fixed 10000 threshold. The selectivity check (>= 0.3) alone is sufficient to prevent the OOM case of scanning 30%+ of the index table.

Summary of changes

  • pkg/sql/plan/apply_indices.go: In tryIndexOnlyScan, replace InFilterCardLimitNonPK with GetInFilterCardLimitOnPK(service, tableCnt) in the non-equality leading condition guard.

🤖 Generated with Claude Code

The guard added in matrixorigin#24309 to prevent OOM from non-selective range scans
used InFilterCardLimitNonPK (10000) as the outcnt threshold in
tryIndexOnlyScan. This is too aggressive for index-only scans which
don't access the base table — queries like sysbench random_ranges
(OR of BETWEENs on an indexed column) produce ~10k rows from 10M and
were incorrectly rejected, causing a 45% TPS regression.

Use GetInFilterCardLimitOnPK (which scales with table size, up to 1M)
instead. The selectivity check (>= 0.3) alone is sufficient to prevent
the OOM scenario of scanning 30%+ of the index table.

Fixes matrixorigin#24335

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@qodo-code-review

Copy link
Copy Markdown
ⓘ You've reached your Qodo monthly free-tier limit. Reviews pause until next month — upgrade your plan to continue now, or link your paid account if you already have one.

Verifies that the tryIndexOnlyScan selectivity guard uses the PK card
limit (scales with table size) instead of the fixed InFilterCardLimitNonPK.
The test confirms: random_ranges (10M rows, outcnt=10000) passes the guard,
while truly non-selective queries (outcnt=2M or selectivity>=0.3) are rejected.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label May 11, 2026
@mergify mergify Bot added the queued label May 11, 2026
@mergify

mergify Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-11 08:37 UTC · Rule: main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-05-11 08:37 UTC · at dc88e4dfb6e735f2e4b405624018ba7fcbb26038 · squash

This pull request spent 20 seconds in the queue, including 2 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage

@mergify
mergify Bot merged commit f622149 into matrixorigin:main May 11, 2026
23 of 24 checks passed
@mergify mergify Bot removed the queued label May 11, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…es regression (matrixorigin#24338)

PR matrixorigin#24309 added a selectivity guard in `tryIndexOnlyScan` to prevent OOM from non-selective secondary index range scans. However, the guard used `InFilterCardLimitNonPK` (10000) as the outcnt threshold, which is too aggressive for index-only scans.

For sysbench `random_ranges` (10 random `BETWEEN` ranges on column `k` with a secondary index, 10M row table):
- The query only reads the indexed column → ideal for index-only scan
- Estimated outcnt ≈ 10000 (10 ranges × ~1000 rows each)
- The guard rejects the index-only scan, forcing a full table scan → **45% TPS regression**

Index-only scans don't access the base table, so the OOM scenario (excessive metadata/bloom filter re-reads from base table join) doesn't apply. The fix uses `GetInFilterCardLimitOnPK` (scales with table size, up to 1M) instead of the fixed 10000 threshold. The selectivity check (`>= 0.3`) alone is sufficient to prevent the OOM case of scanning 30%+ of the index table.

Approved by: @ouyuanning
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…es regression (matrixorigin#24338)

PR matrixorigin#24309 added a selectivity guard in `tryIndexOnlyScan` to prevent OOM from non-selective secondary index range scans. However, the guard used `InFilterCardLimitNonPK` (10000) as the outcnt threshold, which is too aggressive for index-only scans.

For sysbench `random_ranges` (10 random `BETWEEN` ranges on column `k` with a secondary index, 10M row table):
- The query only reads the indexed column → ideal for index-only scan
- Estimated outcnt ≈ 10000 (10 ranges × ~1000 rows each)
- The guard rejects the index-only scan, forcing a full table scan → **45% TPS regression**

Index-only scans don't access the base table, so the OOM scenario (excessive metadata/bloom filter re-reads from base table join) doesn't apply. The fix uses `GetInFilterCardLimitOnPK` (scales with table size, up to 1M) instead of the fixed 10000 threshold. The selectivity check (`>= 0.3`) alone is sufficient to prevent the OOM case of scanning 30%+ of the index table.

Approved by: @ouyuanning
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…es regression (matrixorigin#24338)

PR matrixorigin#24309 added a selectivity guard in `tryIndexOnlyScan` to prevent OOM from non-selective secondary index range scans. However, the guard used `InFilterCardLimitNonPK` (10000) as the outcnt threshold, which is too aggressive for index-only scans.

For sysbench `random_ranges` (10 random `BETWEEN` ranges on column `k` with a secondary index, 10M row table):
- The query only reads the indexed column → ideal for index-only scan
- Estimated outcnt ≈ 10000 (10 ranges × ~1000 rows each)
- The guard rejects the index-only scan, forcing a full table scan → **45% TPS regression**

Index-only scans don't access the base table, so the OOM scenario (excessive metadata/bloom filter re-reads from base table join) doesn't apply. The fix uses `GetInFilterCardLimitOnPK` (scales with table size, up to 1M) instead of the fixed 10000 threshold. The selectivity check (`>= 0.3`) alone is sufficient to prevent the OOM case of scanning 30%+ of the index table.

Approved by: @ouyuanning

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

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf Regression] sysbench random_ranges tps -45% after #24309

3 participants