Repository navigation
Disable TopK dynamic filtering when read-in-order applies - #100235
Conversation
When the sort column in `ORDER BY ... LIMIT` matches the first column of the storage's sorting key, the read-in-order optimization reads data in sorted order and relies on early pipeline cancellation via `LIMIT` to minimize `rows_read`. The TopK dynamic filtering (prewhere `__topKFilter`) is counterproductive in this case: once the threshold is established after the first few rows, the prewhere rejects all subsequent rows (they are beyond the threshold in sorted order), preventing early cancellation and causing a full table scan. This was exposed by PR #99033 which removed the type restriction that previously prevented TopK optimization for String columns. https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=b949c229302bf2d8153647c38452ddffbb04cb66&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%202%2F2%29 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [b066a32] Summary: ✅ AI ReviewSummaryThis PR disables TopK dynamic filtering for PR Metadata
Exact replacement text: ### Changelog category (leave one):
- Performance Improvement
Suggested entry text: ### Changelog entry (a [user-readable short description](https://github.com/ClickHouse/ClickHouse/blob/master/docs/changelog_entry_guidelines.md) of the changes that goes into CHANGELOG.md):
Disable `TopK` dynamic prewhere filtering when `optimize_read_in_order` is used on the first `MergeTree` sorting-key column for `ORDER BY ... LIMIT`, avoiding unnecessary full scans in this query shape.Findings
Tests
ClickHouse Rules
Final VerdictStatus: Minimum required actions:
|
Tests that `__topKFilter` prewhere is not injected when `ORDER BY` matches the MergeTree sorting key prefix and `optimize_read_in_order` is active. Verifies correctness, plan shape, and bounded `read_rows`. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…FLUSH LOGS` Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 100.00% (20/20, 0 noise lines excluded) |
PR #99033 removed the type restriction that prevented TopK optimization for non-numeric types (like String). This exposed a counterproductive interaction between TopK dynamic filtering and read-in-order optimization.
When both are active on the same sorting key column, the TopK prewhere (
__topKFilter) rejects all rows after the threshold is established (since data is read in sorted order), preventing early pipeline cancellation viaLIMITand causing a full table scan instead of reading just a few blocks.The fix disables TopK dynamic filtering when
read_in_orderis enabled and the sort column matches the first column of the storage's sorting key.https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=b949c229302bf2d8153647c38452ddffbb04cb66&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%202%2F2%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Version info
26.4.1.91