Repository navigation
WHATIF projections scan budget - #121766
Conversation
|
Workflow [PR], commit [8650448] Summary: ✅
AI ReviewSummaryThis PR adds sampling to PR Metadata
Findings
Final VerdictStatus: Minimum required actions:
LLVM Coverage ReportMeasured on commit 8650448.
Changed lines: Changed C/C++ lines covered: 359/374 (95.99%) · Uncovered code |
…d pick sampled granules at hashed positions
…ojection-forcing settings
…ection scan reach the read limit exactly
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes8 object files changed (+26.96 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units11 translation units recompiled, 41 s compile time in total, 11 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.05 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…ct has to follow it instead
…ed projection estimates read such ranges
… the debug flaky check
nihalzp
left a comment
There was a problem hiding this comment.
If I understand correctly, the proposed estimation algorithm after sampling is the following:
After we sample granules across parts within the budget, the sampled rows of each part are treated as a small copy of the part. They are sorted by the projection key and cut into granules whose size is shrunk by the sampling ratio, so that the copy has as many granules as the real part. If the part (100,000 rows) has 10,000 granules and the sample has 1,000 rows, each granule would hold 0.1 rows, but the code rounds the size to whole rows and clamps it to at least one row. So the copy ends up with 1,000 granules instead of desired 10,000, and each of them stands for 10 real granules. The copy's index is then pruned with the query's condition and the surviving granules are counted as the projection's marks, so the count comes out 10 times too low.
CREATE TABLE t (a UInt64, b UInt64, c UInt64) ENGINE = MergeTree ORDER BY a
SETTINGS index_granularity = 100, index_granularity_bytes = 0, min_bytes_for_wide_part = 0;
INSERT INTO t SELECT number, cityHash64(number) % 1000, number FROM numbers(2000000)
SETTINGS max_insert_block_size = 2000000, min_insert_block_size_rows = 2000000;
-- The same data with the projection materialized, to get the real numbers.
CREATE TABLE t_real AS t;
ALTER TABLE t_real ADD PROJECTION p_b (SELECT a, b, c ORDER BY b);
INSERT INTO t_real SELECT * FROM t SETTINGS max_insert_block_size = 2000000, min_insert_block_size_rows = 2000000;
CREATE HYPOTHETICAL PROJECTION p_b ON t (SELECT a, b, c ORDER BY b);
EXPLAIN WHATIF max_rows_to_scan = 0 SELECT sum(c) FROM t WHERE a < 100000 AND b < 100;
-- marks: 2011, verdict: not chosen, sampled_marks: 20000 / 20000
-- Correct, this is the full scan.
EXPLAIN WHATIF max_rows_to_scan = 1000 SELECT sum(c) FROM t WHERE a < 100000 AND b < 100;
-- marks: 307, marks_span: 0 to 736, verdict: chosen, sampled_marks: 30 / 20000
-- Expected: about 2011 with the span containing it, and verdict: not chosen.
-- sample_step = 689, so a scaled granule is 100 / 689 = 0.15 rows and is clamped to 1 row.
EXPLAIN WHATIF max_rows_to_scan = 20000 SELECT sum(c) FROM t WHERE a < 100000 AND b < 100;
-- marks: 1954, marks_span: 0 to 4004, verdict: too close to call, sampled_marks: 200 / 20000
-- Expected: verdict: not chosen. The scaled granule is exactly 1 row here and nothing is rounded,
-- but the rounding term still widens the span by the whole estimate on each side.
-- What the optimizer does on the materialized table:
EXPLAIN indexes = 1 SELECT sum(c) FROM t_real WHERE a < 100000 AND b < 100;
-- ReadFromMergeTree (default.t_real), Granules: 1000/20000. The projection is not chosen.
EXPLAIN indexes = 1 SELECT sum(c) FROM t_real WHERE a < 100000 AND b < 100 SETTINGS prefer_optimize_projection = 1;
-- ReadFromMergeTree (p_b), Granules: 2011/20000. The projection read is 2011 marks.One way to fix the problem is to scale the sample up instead of shrinking the copy. Continuing the example above, we have a sample of 1,000 rows from a part of 100,000 rows. We sort the sample by the projection key as usual, and since the sample is 1% of the part, each sampled row stands for 100 real rows. The real projection part would have 10,000 granules of 10 rows, so instead of cutting the sample into granules, we build the index of those 10,000 granules directly and take each granule's key from the sample. Granule k starts at real row 10 * k, and that row falls into sampled row k / 10. As a result, granules 0 to 9 all take the key of the first sampled row, granules 10 to 19 take the key of the second, and so on. The index therefore has 10,000 entries, the same number as the real one, in runs of 10 equal keys. Then we continue as usual: we prune this index with the query's condition exactly as the code does today and count the granules that survive.
|
|
||
| **Settings** | ||
|
|
||
| - `max_rows_to_scan` — how many rows a projection estimate may read before it switches to a sample of granules. Default: `10000000`. `0` means no limit; `max_rows_to_read` can lower it. A sample reads at least about 30 granules and one per part, and when `max_rows_to_read` does not allow that the estimate is `unsupported`. |
There was a problem hiding this comment.
I feel max_rows_to_scan is overly broad even though it's only applicable to projection. Unless we plan to expand this setting later to cover other things as well, I would propose using projection_scan_budget_rows or something similar.
…ts ran past 180 s with S3 storage ClickHouse/clickhouse-private#76917
Related: #118233
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
EXPLAIN WHATIFno longer reads the whole table to estimate a hypothetical projection: pastmax_rows_to_scanrows (10 million by default) it estimates from a sample of granules (at least about 30, and one per part) and reports the resulting range inmarks_span, and amax_rows_to_readlimit now lowers that budget instead of leaving the projection without an estimate.Workflow [PR]
Sync PR [sync-upstream/pr/121766]
Version info
26.10.1.1170-master(included in26.10and later)