Skip to content

perf(plan): cost bounded DELETE lookups using index cardinality - #33044

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/31618-delete-full-key-cost
Nov 21, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/31618-delete-full-key-cost

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #31618. Builds on #31738.

What this PR does / why we need it:

A bounded DELETE still falls back to scanning a hidden index when sampled NDV underestimates the cardinality of its full primary key. In the regression fixture, a 1,000-key bound is selective against a 97,000-row index but hits the previous cost cutoff with an NDV of 1,000.

For the existing late runtime filter path, use known index row count as a lower bound on the NDV estimate when the match is on the complete stored primary key. Each exact lookup key can match at most one visible index row. The 10% selectivity cutoff, key and allocation budgets, index eligibility, join direction, and independent single-CN placement proof still apply.

Validation

  • Current main reproduced the original 100,000-row setup and reduced the final target index scan to 1,000 rows across four blocks. The original v4.2.5 report measured 98,000 rows across twelve blocks. The DELETE removed exactly 1,000 base rows.
  • The real SQL planner fixture with controlled stale NDV fails against the previous production source with a missing index runtime filter. It passes on this revision. The exact 10% cutoff is tested within the IN key budget, and unknown table cardinality retains the original costing.
  • In two isolated SQL runs with the same injected NDV of 1,000 for the target filter, changing only the cost rule reduced the scan from 98,000 rows / 12 blocks to 1,000 rows / 3 blocks. Both runs removed 1,000 rows and restored all rows from a second, rolled-back DELETE. The injected estimate isolates the planner change and does not establish how often live statistics take that value.
  • Full short race suites passed for planner and HashBuild. Final focused bounded DELETE tests passed under race. Incremental formatting, err-check, planner vet, golangci-lint (0 findings), and molint passed.

@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 →

@mergify

mergify Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: dbd549f94fabf7d8262a38ae22754815474237c7

Review completed. Read the assessment.

@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.

mo-bot review · mo-bot-local · review

Commit: dbd549f94fabf7d8262a38ae22754815474237c7

Assessment: approve

The late DELETE retry now uses known index cardinality to avoid rejecting selective full-primary-key lookups when sampled NDV is too low. It reuses the existing runtime filter path and preserves the strict 10% cutoff, key budget, and single-CN placement checks.

Model: gpt-6.2-sol / xhigh

Session: 01a1ee80-0d82-7030-a965-b51f496c3054 · slot 2 · generation 1

Validation:

  • Verified the frozen patch, pinned policy, native inputs, and isolation preflight. Reconciled all three discussion entries and matched the retained logs to their receipts in validation-reconciliation.json.

  • Focused planner and HashBuild tests passed under race, including the exact 10% cutoff, unknown cardinality, forced distribution, and runtime memory fallback. See focused-race.stdout and receipt 1795274992324677592-2.

  • With only runtime_filter.go replaced by its pinned-base version, the same SQL planner test failed at the expected missing index probe assertion. See base-control.stdout and receipt 1795274988710994011-2.

Limitations:

  • The scan reduction, deletion count, and rollback results come from the author's controlled SQL comparison. Local execution covered planner and HashBuild tests.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Independent QA challenge: not passing yet

I ran new probes against base f1ffbaa and head dbd549f. Existing unit tests were not used as QA evidence.

Area Result Evidence
Planner correctness PASS within tested scope 13 new scenarios passed, including complete/composite/partial/non-PK keys, stale and zero NDV, unknown/default/zero/NaN cardinality, the exact 10% boundary, key-budget overflow, reversed equality order, and repeated admission. Base/head differed only where intended, and no unexpected probe-stat mutation was observed.
Performance FAIL / improvement not demonstrated A new persistent 100,000-row workload with rolled-back 1,000-row DELETEs was run in ABBA order, four measured repetitions per binary. Base median/mean DELETE latency was 21.596834/21.813677 ms; head was 22.504000/22.099469 ms. Target input was 100000 rows and 13 blocks in every repetition, with the target runtime-filter probe absent in every plan.
Unhappy paths PARTIAL Several guard/boundary paths passed in the new planner probe. A separate unhappy-path fixture was discarded because its high-NDV positive control also failed admission, so its output is not evidence of a product defect. Cancellation, timeout, resource cleanup, and newly admitted SQL rollback/restart remain unverified.

The measured latency difference must not be interpreted as a confirmed regression: the changed path was not reached in either binary. Conversely, this workload cannot support a performance-improvement claim. Every measured DELETE affected 1,000 rows and rollback restored 100,000 rows, but that does not validate execution through the newly admitted filter path.

Before QA sign-off, please provide a real SQL workload whose EXPLAIN ANALYZE confirms the intended full-index-PK runtime filter on head, then rerun the base/head performance comparison and end-to-end DELETE correctness checks through that path. The unknown-cardinality control was not executed in this SQL workload.

@XuPeng-SH

XuPeng-SH commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: dbd549f94fabf7d8262a38ae22754815474237c7

New discussion was assessed; the previous review remains unchanged.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Corrected independent SQL QA: PASS for the original issue workload

This supersedes my earlier QA comment. The earlier SQL results were invalid because port 6001 was already occupied by a Docker MatrixOne instance (v4.2.8, git 10d204b), so the workload was not executed by the base/head binaries. I apologize for that incorrect conclusion.

I rebuilt and reran the base/head processes on an isolated port, using the original #31618 candidate schema and history. No existing unit tests were used as QA evidence.

Workload:

  • Base f1ffbaa; head dbd549f.
  • 100,000 initial rows and 98,000 rows after the two historical DELETEs.
  • Target index: idx_user_id; injected NDV=1000 and table_cnt=98000.
  • DELETE FROM candidate WHERE expires_at < NOW(6) LIMIT 1000.
  • ABBA ordering, four measured repetitions per binary. Every measurement was rolled back after validating its effects.
Metric Base Head
Runtime-filter probe Absent in all 4 plans Present in all 4 plans
Target input rows 98,000 in all 4 runs 1,000 in all 4 runs
Target input blocks 12 in all 4 runs 4 in all 4 runs
Median DELETE latency 9.933021 ms 6.238167 ms
Mean DELETE latency 10.483000 ms 6.437479 ms
Latency samples (ms) 12.976834, 10.465375, 9.089125, 9.400667 7.962958, 7.093834, 5.382500, 5.310625

Head reduced median latency by approximately 37.2% and mean latency by approximately 38.6%, while reducing the target scan from 98,000 rows to 1,000 rows.

Correctness checks passed in every measured run: exactly 1,000 rows were deleted, 97,000 rows remained before rollback, and rollback restored 98,000 rows.

The unknown-cardinality control also behaved conservatively: with table_cnt=0, both base and head omitted the target runtime filter and scanned 98,000 rows / 12 blocks.

Conclusion: performance and SQL DELETE/rollback correctness pass for this original issue workload. This conclusion is scoped to the exercised path and controls.

This branch was successfully deployed

No deployments
ci — aa191037 Deployed Nov 21, 2026 by XuPeng-SH
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 kind/enhancement size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants