Repository navigation
fix(cost): charge an exact distinct count per value and pair lookup that misses the cache; declare the production workloads' column-wide distinct users - #1368
Merged
Conversation
… up, and its column's declared distinct values cap its value table
…hat misses the cache and per inserted value; declare the production workloads' column-wide distinct users
zzylol
force-pushed
the
fix/exact-distinct-cost-refit
branch
from
November 16, 2026 17:03
266d2d6 to
2021937
Compare
…er-group distinct bound
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
COUNT(DISTINCT user_id)too much when a source has many rows per distinct value. On the production SQL shape it predicts 273.7 s per evaluation for the exact counts against 200.2 s for KMV (k = 4,323), but the exact counts measure 1.8x faster (12.3 s against 21.8 s over 12,000,000 rows). The shape is 120,000,000 rows, 1,000 (region, service) groups and 1,000,000 users. Once draft PR fix(accuracy): split δ over the readout values the covered output values depend on #1360 corrects the δ split, the planner picks KMV here.distinct_values_per_label: {"user_id": 1_000_000}.max_distinct_per_evaluationkeeps its documented per-group meaning.production_sql_distinct_region_service_counts_exactlypasses. At therollup_controlledshape, KMV measures 1.26x faster than exact at 60,000,000 rows, so the planner's KMV choice there is correct (see Decisions).Review guide
mainat 8e65fca. The PR is not stacked. It was also checked on top of draft PR fix(accuracy): split δ over the readout values the covered output values depend on #1360 (fix/delta-split-over-dependent-readoutsat 4f069ac).exact_distinct_value_operations_per_rowand its constants incrates/cost/src/node/operations_per_unit.rs. Then read the two workload declarations, inproduction_rollup_workloads::data()andtools/dag-viewer/memo-examples/sql/rollupheavy-t11-service-endpoint-5m.json.main. Commit 2 (2021937) is the fix and the workload declarations.max_distinct_per_evaluationand declares the column-wide fact in the workloads.Behavior: before → after
The workload is
production_rollup_workloads::data():httpat 2,000,000 rows/s, 10,000 series and 120,000,000 rows held. Each group has at most 1,000,000 distinct users per evaluation (max_distinct_per_evaluation), and this PR also declares 1,000,000 for the wholeuser_idcolumn (distinct_values_per_label). It runs two queries every 60 s at a 5% relative per-value target:Aggregateby (region, service)Aggregateby serviceThe measurements used the executor at release, one pinned core, median of 3, over 12,000,000 rows. At the measured shape the exact charge is 0.98x (by (region, service)) and 1.01x (by service) of the measured time. The per-row charge at 120,000,000 rows is about 5% lower, because the inserted values and pairs are shared over 10x the rows. Before this PR the exact charge was 2.2x the measured time.
Before this PR the charge grew because the planner passed 120,000,000 distinct values. Without a column-wide declaration it bounds the value table by the per-group bound times the groups (1,000,000 × 10,000) and by the rows read, so every row was taken as differing. The value lookup was then charged as at 3,000,000 values (918 per row). With the declared 1,000,000 users the table holds 1,000,000 values.
This serves the user need that the measured-best plan is selected (MVP correctness of plan choice).
Interfaces
None public. All changed items are crate-private, in
crates/cost/src/node/operations_per_unit.rs:EXACT_DISTINCT_LOOKUP_*andEXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRYare removed because the new form replaces them.node_resource_usage_and_cost(crates/cost/src/node/usage/mod.rs) is unchanged.The two workloads declare one more data fact:
"http": { "series_count": 10_000, "sample_interval": 5, "column_signs": {"value": "non_negative"}, "max_distinct_per_evaluation": 1_000_000, + "distinct_values_per_label": {"user_id": 1_000_000}, "rows_held": 120_000_000, }Usage. The flow is the workload above →
run_planning→ Stage 3plan_cost→node_resource_usage_and_cost. For each exactAggregate { measures: [Cardinality { cols: [user_id] }] }that call:run_planningthen selects the two exactAggregates (117.2 s) over KMV k = 4,323 (200.6 s).Algorithm changes
Exact distinct-count cost
Problem. 12,000,000 rows of 1,000,000 users, ungrouped, measure 381 ns per row and were charged 587 (1.54x). 1,000 users by
instance(10,000 groups, 7.0 M pairs) measure 179 and were charged 139 (0.78x). The square-root term over-charges large value tables. The same constant was charged for a value insert and a pair insert, so a pair set that has outgrown the cache was not charged at all.Before.
41 + 0.53 √min(values, 3 M) + 72 (min(values, rows) + pairs) / rows.After.
values = min(declared, max_distinct_per_evaluation × groups, rows), innode_resource_usage_and_cost, unchanged.pairs = G · values · (1 − e^{−rows/(G·values)}), unchanged.v = clamp(values, 1e3, 1e7), the charge per row is59 + 13 log2(v/1e3) + 207 miss(v, 2e5) + 79 miss(pairs, 5e5) + (356 min(values, rows) + 13 pairs) / rows, withmiss(x, K) = max(0, 1 − K/x)(cache_miss_fraction).Fit.
Data: 119 exact
COUNT(DISTINCT)aggregates. The new grid is 10,000 series, 10 rows per series per batch: 1.2 M, 12 M and 36 M rows × 1e3, 1e4, 1e5, 1e6 and 1e7 distinct values × ungrouped, 10, 100, 1,000 and 10,000 groups (1 or 2 keys), 75 aggregates. The earlier grid is 1,000 series, 300 k and 3 M rows, 100 to 1e8 values, 1 to 1,000 groups, 44 aggregates.The key, measure, visit and output-row terms are fixed at their constants. The fit minimizes squared log error and uses the features the planner passes (values capped at the rows).
Form chosen by leave-one-setting-out error. A setting is a (distinct values, rows, series) triple, so all of its groupings are held out together. Among about 45 forms (square root, log2, saturating and cache-miss terms, one or two insert terms, grids of cache sizes), the chosen form has LOSO RMS log error 0.122 (largest single error 0.32×). Leaving out each group count gives 0.116, each distinct count 0.148 and each row count 0.128. The old form, refit, scores 0.202.
Fitted constants: 59.2, 12.9, 207, 79.2, 356 and 13.3 (cache sizes 200,000 values and 500,000 pairs).
Rounded constants: measured ÷ charged is 0.76–1.32 (RMS log 0.109) over all 119 aggregates. The old constants give 0.61–1.58 (0.224).
Held-out shapes. Five shapes outside both grids were measured afterwards (12 M rows, one idle pinned core, median of 3; controls reproduced the grid within 3–4.5%). The new charge stays within 0.89–1.11 of the measured time, against 0.63–1.18 for the old one:
Correctness. The charge rises with the values, with the pairs and with the inserted entries, and never falls below 59 per row.
an_exact_distinct_count_costs_per_value_and_pair_it_looks_upchecks five measured shapes at 0.8–1.25x. The two older measured-point tests still hold.Complexity. O(1) per node, unchanged.
Assumptions and limits.
Aggregateover raw rows. So no merge term was fitted (see Follow-ups).Decisions and open questions
Decided
max_distinct_per_evaluationkeeps its per-group meaning. Its doc says "Each group's own items count separately: the bound is per output row". An earlier revision of this PR read it as a bound over all groups together, and capped the value table at it. Example of why that is wrong: 10,000instances with disjoint users, at most 1,000 per instance per evaluation, so 10,000,000 users in total. A deployment may validly declaremax_distinct_per_evaluation: 1000. ForGROUP BY instanceover 12,000,000 rows:Instead, a workload whose users are shared across groups declares the column-wide fact
distinct_values_per_label, which the cost already reads. The two production distinct-count workloads now declare{"user_id": 1_000_000}. With fix(accuracy): split δ over the readout values the covered output values depend on #1360, this gives the same plans as the total reading: the SQL test picks exact, and the rollup test picks KMV on both queries.an_exact_distinct_count_reads_at_most_its_column_s_declared_distinct_valueschecks that the declared fact caps the value table, and that the per-group bound alone does not.rollup_controlled::production_distinct_rollup_counts_exactlywith fix(accuracy): split δ over the readout values the covered output values depend on #1360. The best plan there depends on the rows per state, so this PR does not force exact. The test plansCOUNT(DISTINCT user_id)by service and by (service, endpoint) over 600,000,000 rows per evaluation. Measured on the executor (×10 to one evaluation):Aggregateby (service, endpoint)Aggregateby serviceThe form was chosen by leave-one-setting-out error over both grids, not by any test. One existing test bound was widened as a result (see Verification).
Verification
main8e65fcacargo test --release -p asap-costcargo test --release -p asap-integration-tests --test integration -- production_sql_distinct_region_service_counts_exactly production_distinct_rollup_counts_exactlycargo fmt --all --check,cargo clippy --release -p asap-cost --all-targetsasap-typesunused-import warning)Columns are passed / failed / ignored.
New and changed tests:
an_exact_distinct_count_costs_per_value_and_pair_it_looks_up(new, fails onmain) checks five measured 12 M-row shapes (1e3 to 1e7 users, ungrouped to 10,000 groups), each charged within 0.8–1.25x of its measured time.maincharges 0.78–1.54x.an_exact_distinct_count_reads_at_most_its_column_s_declared_distinct_values(new) checks that a count byinstanceover 120 M rows, withuser_iddeclared at 1 M values and a per-group bound of 1 M, is charged as over the 1 M declared values. With the per-group bound alone it is charged more (as over the rows read).a_grouped_exact_distinct_count_pays_its_key_and_visits_once(changed bound, 0.8–1.25 → 0.75–1.35). The bound is the fit's measured ÷ charged range over 119 aggregates. It is wider because the 100-user point is noisy: over the same 300,000 rows of 100 users, the count byjobmeasures 60.2 ns per row and the count byregion86.1, 1.43x apart. Every count under 1,000 values is charged as at 1,000, so the 60.2 point is charged 1.33x, near the edge.Earlier plan sweep. All 203 workloads were planned on
mainand on an earlier revision of this branch throughrun_default_search_over: #1363's grid (120), the showcase gate (24), the held-out set (48), the 2,000-series set (10) and the viewer'srollupheavy-t11. 203/203 select the same plan, node by node. But none of the 202 others contains a distinct count. The 10cdistworkloads arecount(count by (instance) …), which are exact Count windows. So onlyrollupheavy-t11reaches the changed code. Its exact plan was already selected, and its cost fell from 37,323 to 16,331 per second. That workload now declares the same 1,000,000 users, so the values it passes are unchanged from that revision. The evidence that the new charge ranks plans correctly is therefore the fit, the held-out shapes table above, and the two production shapes measured with free, exact-only and KMV-only arms.The measurements and fit scripts are kept locally.
Design scope
cache_miss_fraction(private fn,crates/cost/src/node/operations_per_unit.rs) belongs to the Stage 3 cost scope, per-operation cost kernels. It is the general cost of a random lookup into a hash table that has outgrown the cache, and is not specific to distinct counts. It sits beside the other per-unit operation costs and reads only its arguments.Follow-ups
HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY). This is the largest residual on the grid and is not changed here.