Skip to content

fix(cost): charge a counter-matrix sketch's state allocation, heap re-estimates, readout and merge, measured on the executor - #1382

Merged
zzylol merged 3 commits into
mainfrom
fix/query-time-sketch-size-cost
Nov 17, 2026
Merged

zzylol merged 3 commits into
mainfrom
fix/query-time-sketch-size-cost

Conversation

@zzylol

@zzylol zzylol commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: a Count-Min, Count Sketch or UnivMon built at query or ingestion time was charged per row only, so its width cost nothing. A 7,500 × 81 Count Sketch with heap for 100 URLs was charged 0.151 ms per evaluation, the same as a 300 × 81 one.
  • Change: the cost model now charges each state's counter allocation, the heap's re-estimate of its residents on every update (which this repo's executor kernel does today), the heap's top-k read, and a merge per counter. All are fitted on 235 executor builds, 158 readouts and 126 merges. A heap's read and merge now count the distinct items it holds, as its update already did.
  • Effect: that build is now charged 4.40 ms (measured 3.55); the 300 × 81 one 3.18 ms (measured 3.08). All 25 top-k sketch plans in the sketch-heavy set now pick the exact plan; their ≤ 13% benefits came from the under-charge. Closes [Bug] The cost model does not charge for a sketch's width: a query-time Count Sketch costs the same at 24,300 or 607,500 counters #1381.

Review guide

  • Base: main (5ebca6c11, after the KMV refit). Rebased and ported onto main's NodeCostTerms: every new constant is a CostConstant. The pre-port head is on backup/fix/query-time-sketch-size-cost-e71782461.
  • Commits: 3832afa7b the charges and their tests (ported); 6004805b6 regenerates one sketch snapshot (see Verification); 34dea5d69 applies the final review.
  • Read first: update_terms_per_row, merge_terms_per_state and estimate_terms_per_group in crates/cost/src/node/operations_per_unit.rs, then the new constants above them and their CostConstant variants in crates/cost/src/node/cost_terms.rs.
  • Wiring: in crates/cost/src/node/usage/mod.rs, the SummaryAgg and SummaryTumblingWindowChain arms add one allocation per state and record the distinct values their state folds in StateInputRows, which the SummaryEstimate and SummaryMerge arms pass on.
  • Needs careful review: the constants and their forms. The fit's data and scripts are summarized under Algorithm changes.

Behavior: before → after

topk(10, sum by (url) (rate(http_requests_total[1h]))), with these settings:

  • 100 URLs, 1 instance each, 1 Hz, every 60 s;
  • an L2 target of ε = 0.02, δ = 0.1.
Before After
Selected plan Exact rate chain, then a query-time Count Sketch with heap, 7,500 × 81 (607,500 counters), read for the top 10 Exact rate chain, merged, then an exact top 10
Plan cost per second 0.1677 (exact-only 0.1697) 0.1697
The sketch build's charge per evaluation 0.151 ms 4.40 ms
Measured on the executor (100 rows into 1 state) 3.55 ms 3.55 ms

Interfaces

No new public type. CostConstant (public, crates/cost/src/node/cost_terms.rs) gains 12 variants, in the constants' declaration order; their JSON names are the snake_case of each constant (e.g. "cms_heap_operations_per_estimate"). Everything else is crate-private to asap-cost. StateInputRows::distinct_values already exists on main (merging keeps the largest); this PR fills it from a keyed summary's item column.

crates/cost/src/node/operations_per_unit.rs

+pub(crate) const COUNTER_MATRIX_UPDATE_OPERATIONS_PER_COUNTER: f64 = 19.0;
+pub(crate) const UNIVMON_UPDATE_OPERATIONS_PER_COUNTER: f64 = 95.0;
+/// Per estimate a Count-Min Sketch with heap computes per row: one per heap resident
+/// (the executor's kernel calls `rescore_heap` after each update) plus the row's own item.
+pub(crate) const CMS_HEAP_OPERATIONS_PER_ESTIMATE: f64 = 150.0;
+pub(crate) const CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER: f64 = 33.0;
+pub(crate) const COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATE: f64 = 58.0;
+pub(crate) const COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER: f64 = 36.0;
+/// Per counter a counter-matrix sketch's state allocates and zeroes, once per state built.
+pub(crate) const SKETCH_ALLOCATION_OPERATIONS_PER_COUNTER: f64 = 2.1;
+pub(crate) const COUNTER_MATRIX_MERGE_OPERATIONS_PER_STATE: f64 = 2_300.0;
+pub(crate) const COUNTER_MATRIX_MERGE_OPERATIONS_PER_COUNTER: f64 = 2.9;
+pub(crate) const HEAP_MERGE_OPERATIONS_PER_RESIDENT_COUNTER: f64 = 53.0;
+pub(crate) const HEAP_READ_OPERATIONS_PER_GROUP: f64 = 4_000.0;
+pub(crate) const HEAP_READ_OPERATIONS_PER_RESIDENT_COUNTER: f64 = 55.0;
 /// Kept, now only for Hydra, HyperLogLog and samples (not measured on the executor).
 pub(crate) const SKETCH_UPDATE_OPERATIONS_PER_COUNTER: f64 = 18.0;
 /// Kept, now only for Hydra and HyperLogLog merges.
 pub(crate) const SKETCH_MERGE_OPERATIONS_PER_BYTE: f64 = 1.0;
+fn sketch_state_counters(parameters: &SketchParams) -> f64;
+/// What allocating and zeroing one state of `parameters` costs; a build pays it per state.
+pub(crate) fn state_allocation_terms(parameters: Option<&SketchParams>) -> NodeCostTerms;
+fn heap_residents_per_update(heap_size: u32, rows_per_state: Option<u64>, distinct_items: Option<u64>) -> f64;
+fn heap_residents(heap_size: u32, input_rows: Option<StateInputRows>) -> f64;
+/// The expected distinct items among `rows` uniform draws (also used by the KMV merge).
+fn distinct_items_seen(rows: f64, distinct_items: Option<u64>) -> f64;
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[derive(Debug, Clone, Copy, PartialEq)]
 pub(crate) enum StateLastWrittenBy {
-    Merge,
-    Updates { rows_per_state: Option<u64> },
+    Merge { input_rows: Option<StateInputRows> },
+    Updates { input_rows: Option<StateInputRows> },
 }

crates/cost/src/node/cost_terms.rs

 pub enum CostConstant {
     ...
     SketchUpdateOperationsPerCounter,
+    CounterMatrixUpdateOperationsPerCounter,
+    UnivmonUpdateOperationsPerCounter,
+    CmsHeapOperationsPerEstimate,
+    CmsHeapOperationsPerEstimatedCounter,
+    CountSketchHeapOperationsPerEstimate,
+    CountSketchHeapOperationsPerEstimatedCounter,
+    SketchAllocationOperationsPerCounter,
     ...
     SketchMergeOperationsPerByte,
+    CounterMatrixMergeOperationsPerState,
+    CounterMatrixMergeOperationsPerCounter,
+    HeapMergeOperationsPerResidentCounter,
     ...
     SketchEstimateOperationsPerGroup,
+    HeapReadOperationsPerGroup,
+    HeapReadOperationsPerResidentCounter,
     ...
 }

crates/cost/src/node/usage/mod.rs:

-    pub(crate) fn unchanged_column(&self, weight: &SummaryInputExpr) -> Option<&str>;
+    pub(crate) fn unchanged_column(&self, expr: &SummaryInputExpr) -> Option<&str>;
  • folded_distinct_values reads a keyed summary's item column (the url a top-k heap counts), and its weight column otherwise.
  • SummaryAgg adds group_count × state_allocation_terms; SummaryTumblingWindowChain adds the same per emitted state.
  • SummaryEstimate passes the producer's state_input_rows in both StateLastWrittenBy variants, so a heap read after a merge counts the merged state's distinct items.

Usage, end to end, for the workload above:

// The user's query: topk(10, sum by (url) (rate(http_requests_total[1h]))), 100 URLs at 1 Hz, every 60 s,
// with the data fact distinct_values_per_label {url: 100}.
// Pass 1 offers a CountSketchWithHeap { width: 7_500, depth: 81, heap_size: 10 } candidate built at query time
// over the 100 finalized per-URL rates. Stage 3 costs it through node_usage_and_cost_terms:
let rows_per_state = Some(100);   // 100 rates into 1 state (the reduction keeps no key)
let distinct_items = None;        // folded_distinct_values: the PromQL item is a Tuple of url and the
                                  // series identity, which unchanged_column does not resolve (#1387),
                                  // and the workload declares no max_distinct_per_evaluation
// update_terms_per_row: 29 + (heap_residents_per_update(10, rows, items) + 1) × (58 + 36 × 81)
//   = 29 + (9.53 + 1) × 2_974 ≈ 31_340 per row → 3.13 ms for 100 rows
//   (with the 100 declared URLs resolved, 9.50 residents: the same charge)
// state_allocation_terms: 2.1 × 607_500 = 1_275_750 → 1.28 ms for its one state
// The build is charged 4.40 ms (measured 3.55); the exact top 10 over the same 100 rows is cheaper,
// so Stage 3 selects the exact plan: cost 0.1697 per second (before: the sketch, 0.1677).

Algorithm changes

Cost model: counter-matrix sketch state size, heap and merge

Problem. update_terms_per_row charged 29 + 18 × (depth + 1) per row for every Count-Min, Count Sketch (± heap) and UnivMon, plus nothing per state. Three costs were missing:

  • Allocation: allocating and zeroing width × depth counters per state.
  • The heap: the executor's weighted-frequency kernel (crates/executor/src/summary_kernels/weighted_frequency.rs, update_key) calls rescore_heap() after every update, which re-estimates every heap resident. asap_sketchlib's own heap update estimates only the touched item; the per-row rescore is the kernel's choice, made to keep an older kernel's exact membership. A Count Sketch row at depth 81 with 10 residents measures 33–40 µs; it was charged 1.5 µs. This PR charges what the executor does today. Once the kernel stops rescoring, the four *_HEAP_OPERATIONS_PER_ESTIMATE* constants must be measured again (see Follow-ups).
  • Readout and merge: the readout was a flat 1,000 per group, and the merge an unmeasured 1 per byte.

Across the grid, the old charges were 1–800× under (RMS log error 3.1).

Before → after.

  1. Per row (update_terms_per_row):
    • Count-Min and Count Sketch: 29 + 19 × depth.
    • Heap variants: 29 + (residents + 1) × (a + b × depth), with Count-Min a = 150, b = 33 and Count Sketch a = 58, b = 36. Residents come from heap_residents_per_update: the distinct items seen so far, at most the heap size, averaged over the state's rows.
    • UnivMon: 29 + 95 × counters_updated_per_row.
  2. Per state built (SummaryAgg per group; a chain per emitted state): 2.1 × sketch_state_counters.
  3. Readout of a heap sketch (estimate_terms_per_group): 4,000 + 55 × residents × depth, plus the output row. Residents come from heap_residents: the distinct items the state folded (distinct_items_seen), at most the heap size. UnivMon and the plain sketches keep 1,000 per group plus the 2,500 output row. UnivMon's L2 read measured 0.2–0.3 µs per group with its row at 1,000 states, whatever its width, so it stays over-charged; that is small next to its build.
  4. Merge per input state (merge_terms_per_state): 2,300 + 2.9 × counters, plus 53 × residents × depth for a heap variant.

Fit.

  • Measurement:
    • The executor's per-group build, top-k or L2 readout, and merge-into-one, each timed as its own thread CPU.
    • Median of 3, on one pinned core whose hyperthread sibling stayed under 2% busy. Load average was 3.9–9.3 on 56 cores, recorded before and after every point.
    • The core was spun up for 50 ms before each timed run. Without that, runs of a few ms on a demand-clocked core measured up to 1.8× slow.
  • Grid:
    • 1–1,000 states × 10–100,000 rows × 10–10,000 items, heaps of 10 and 50.
    • CMS+heap at widths 272–2,719 and depths 5–12.
    • CS+heap at widths 300–120,000 and depths 47–155.
    • UnivMon at 624–56,817 columns × 5 rows.
    • Plain Count-Min, its kernel timed alone (it has no executor build path).
    • 17 ingestion-time chain builds, held out of the fit.
  • Forms: each term is the kernel's work. They were compared by leave-one-setting-out error, where a setting is (family, states, rows, items, heap), and by leave-one-shape-out error.
    • Rejected: an allocation regime split by total bytes, a page-touch term, and per-family allocation constants. None lowered the error by more than 0.02.
    • For Count Sketch with heap, the heap terms cut the leave-one-setting-out error from 0.58 to 0.36 RMS log.
Term Points Measured ÷ charged (Rust charges) Leave-one-setting-out RMS log Old constants
Build, query time 202 0.55–4.94 0.313 (shape: 0.319) 1.0–797 under (3.1 RMS log)
Build, ingestion (held out) 17 0.62–2.36 — (0.375 RMS log) 4–246
Heap readout, ≥ 10 states 98 0.66–2.65 (0.66–2.14 before residents counted distinct items; see below) 0.263 2.1–89
Merge 126 0.80–2.75 0.196 (family: 0.239) 0.34–3.49

Heap residents at read and merge count distinct items. heap_residents was min(heap, rows); it is now min(heap, distinct_items_seen(rows, distinct)), the same count heap_residents_per_update uses. A state of 1,000 rows of 5 items was charged 5 residents per update but 10 at read and merge; now 5 at all three. The constants are unchanged. This changes the charges at 26 of the 158 grid points, all of them states of 10 rows of 10 keys:

  • Why: at those points the bench cycles the keys (row i gets key i % 10), so every state holds all 10. The uniform-draw count expects 6.32. On the other points the two counts agree.
  • Readout: measured ÷ charged at those points moves from 0.66–1.80 to 0.94–2.65. Over the 98 readouts of 10 or more states, the range goes from 0.66–2.14 to 0.66–2.65, and the RMS log error from 0.296 to 0.363.
  • Merge: the range stays 0.80–2.75; the RMS log error goes from 0.203 to 0.204.
  • Refit: refitting the read and merge constants with the distinct-item count does worse on this grid (readout leave-one-setting-out 0.303 against 0.263), because the bench's cycled keys are what that count misses. So the constants keep the fit on the bench's true residents. Real items arrive as draws, not cycles, and that is what the distinct-item count models.

Correctness. Three tests check this:

  • a_wider_query_time_count_sketch_costs_more_as_the_executor_measures: at width 30,000 against 300, the charge ratio is within the fit's leave-one-setting-out factor of 1.37 of the measured ratio (2.06).
  • counter_matrix_sketches_are_charged_their_measured_time: 15 measured shapes across build, readout, merge and ingestion, each within the fit's range (readout 0.65–2.66, the whole grid's range with distinct-item residents).
  • a_heap_over_few_distinct_items_is_read_and_merged_for_the_items_it_holds: a heap of 10 over 1,000 rows of 5 items is read and merged for 5 residents.

The ranges come from the fit; no constant was tuned to the tests.

Complexity. Each term is O(1) per node. heap_residents_per_update sums 16 slices.

Assumptions and limits.

  • Allocation is the largest error. States of 2.8 MB or more are served either from memory the allocator keeps or from freshly faulted pages. One 120,000 × 47 Count Sketch measures 4.9× its charge; 100 states of 30,000 × 81 measure 0.55×.
  • Readout of one state: a constant 19 µs per execution is not charged, so a single-state readout measures up to 5.5× its charge.
  • Unmeasured families keep their old constants: Hydra, HyperLogLog, and plain Count Sketch (no executor path). A plain Count Sketch takes Count-Min's per-counter update.
  • Machine: one 56-core server; the constants are the defaults a measured table (measured_operation_costs) replaces.
  • PromQL top-k items get no distinct count. unchanged_column resolves only a single Column. A PromQL top-k item is always a Tuple (even of one column, from a closed child), the series-identity tuple (series_identity_item appends __series_identity), or an EntityIdentity. So it falls back to max_distinct_per_evaluation, and to None when the workload does not declare that. Only a SQL single-column item resolves. I chose to state this gap and file [Bug] A top-k heap over a PromQL item (tuple or series identity) is costed as if every row's item differed #1387 rather than resolve a Tuple here: a correct resolution also needs the series identity and EntityIdentity mapped to the series count, which is more than a product of declared values. The surveyed heavy-hitter workloads are charged the same either way (9.53 against 9.50 residents).

Decisions and open questions

  • Decided: StateInputRows::merged keeps the largest of its inputs' distinct values (as main defines it), not their sum: the inputs fold the same column.
  • Needs reviewer decision: a keyed summary's distinct values are now read from its item column, not its weight column. Only KMV used them before, and KMV has no item, so no other plan changes. Today this reaches only single-column (SQL) items; PromQL items are tuples and resolve to None ([Bug] A top-k heap over a PromQL item (tuple or series identity) is costed as if every row's item differed #1387).
  • Decided: one allocation constant for every family, rather than a constant per family or a split by memory regime. The alternatives did not lower the leave-one-out error.

Verification

Run on a 40-core build host, PR 34dea5d69 against main 5ebca6c11:

Scope Command main PR Newly failing
asap-cost cargo nextest run -p asap-cost 113 pass 116 pass none (3 new tests)
asap-cost + asap-integration-tests + asap-devtools cargo nextest run -p asap-cost -p asap-integration-tests -p asap-devtools --no-fail-fast 456 pass / 9 fail / 8 skipped none: the 9 fail on main too
sketch and corpus snapshots cargo test --release -p asap-devtools --test corpus_snapshots -- --ignored 2 pass 2 pass after regenerating one snapshot (below) none
fmt, clippy cargo fmt --all --check; cargo clippy -p asap-cost -p asap-integration-tests -p asap-devtools --all-targets -- -D warnings clean

The 9 failing on both: joint_search_equivalence ×2, materialized_views::materialized_views_equal_the_stored_outputs_main_listed, plan_sharing ×2, promql_to_post_asap::promql_quantile_of_rate_binds_kll_over_rate_accumulator, planner_fuzz ×3.

New tests:

  • a_wider_query_time_count_sketch_costs_more_as_the_executor_measures: a wider Count Sketch costs more, in proportion to the executor's time.
  • counter_matrix_sketches_are_charged_their_measured_time: measured CMS+heap, CS+heap and UnivMon builds (at query and ingestion time), readouts and merges are charged within the fit's range.
  • a_heap_over_few_distinct_items_is_read_and_merged_for_the_items_it_holds: a heap's read and merge count the distinct items it holds.

Snapshot topk-rate-guarantees-ignored-mvp regenerated (by construction). The workload is topk(5, sum by (job) (rate(...))) with accuracy guarantees ignored. The query-time top-k now reads an exact aggregate instead of a Count-Min Sketch with heap (width 28, depth 5, heap 10). Hand-derived per execution, 10,000 finalized rates, 0.1 executions per second. job declares no distinct values, so every heap is full:

  • Count-Min with heap, on main: 10,000 rows × (29 + 18 × 6 = 137) = 1.37 M operations (1.37 ms).
  • Count-Min with heap, this PR:
    • build: 10,000 rows × (29 + 11 × 150 + 11 × 5 × 33 = 3,494) = 34.94 M, plus 170 for the group visit and 294 to allocate 140 counters;
    • readout: 4,000 + 55 × 10 × 5 + 2,500 = 9,250;
    • total about 34.95 M operations (0.003495 core-s/s).
  • The exact arm:
    • sum by job: 10,000 × 20 + 10,000 × 170 + 10,000 × 2,500 = 26.9 M;
    • top-k aggregate: 10,000 × 69 + 170 + 2,500 = 0.69 M;
    • total 27.59 M operations (0.002759 core-s/s).
  • Result: exact is 21% cheaper. The heap charge of about 3.5 µs per row matches its measurement: 3.41 ms for 1,000 rows at depth 5. The snapshot's total CPU, 0.027839 core-s/s, equals the shared rate chain's 0.025080 plus the exact arm's 0.002759. The manifest's covers text now says so.
  • Coverage gap: no plan snapshot selects a heap sketch any more.

Plan survey, main 5ebca6c11 against this PR. Every workload was planned on both with the executor's sketch families, cost model 1, and an exact-only run for the sketch-heavy set. All 357 runs plan. The PR side was run again after the final review fixes and is identical:

  • The 203-workload set: the per-shape sweep's 120, the showcase gate's 24, the held-out 48, the 2,000-series 10 and rollupheavy-t11. Every plan and every cost is unchanged.
  • The sketch-heavy set (77): 46 plans change, the same 46 as before the rebase. Three more keep their plan at a higher cost.
  • Against the earlier run: the only difference is in mixed__l2-and-distinct__*. Main's KMV refit already builds their KMV in ingestion windows, so they now differ only in UnivMon moving to ingestion.

Top-k:

  • All 18 heavy-hitter plans and the 7 mixed plans that read a CMS+heap or CS+heap now plan exact. No top-k sketch benefit survives; the old benefits were 1.00–1.13×.
  • edge-dashboard keeps its UnivMon, now built at ingestion; its CMS+heap goes exact (2.84× → 1.92×).

UnivMon L2:

  • Its update now costs the measured 599 per row, against 137 before.
  • 15 plans move their UnivMon build to ingestion: 5 frequency-moments and 10 mixed.
  • Three 1 Hz workloads of 1–4 resolvers now plan exact.
  • The UnivMon plans' benefits fall from 2.69–5.91× on main to 1.03–3.57×.
Workload main plan main cost (exact-only ÷ cost) PR plan PR cost (exact-only ÷ cost)
frequency-moments__univmon-l2__r1-10hz-6h-every10s-releps0.2-d0.05 UnivMon 624×5, query 3.325 (3.75×) UnivMon 624×5, ingestion 9.415 (1.32×)
frequency-moments__univmon-l2__r1-1hz-1h-every60s-releps0.1-d0.05 UnivMon 624×5, query 0.01021 (2.94×) exact 0.03005 (1.00×)
frequency-moments__univmon-l2__r2-10hz-24h-every300s-releps0.1-d0.05 UnivMon 624×5, query 0.8865 (5.44×) UnivMon 624×5, ingestion 1.894 (2.55×)
frequency-moments__univmon-l2__r2-1hz-5m-every60s-releps0.1-d0.05 UnivMon 624×5, query 0.001809 (2.69×) exact 0.004865 (1.00×)
frequency-moments__univmon-l2__r4-10hz-24h-every300s-releps0.1-d0.1 UnivMon 624×5, query 1.773 (5.72×) UnivMon 624×5, ingestion 3.788 (2.68×)
frequency-moments__univmon-l2__r4-1hz-1h-every10s-releps0.1-d0.05 UnivMon 624×5, query 0.2451 (3.11×) exact 0.7631 (1.00×)
frequency-moments__univmon-l2__r8-10hz-1h-every60s-releps0.05-d0.1 UnivMon 2367×5, query 0.7393 (4.12×) UnivMon 2367×5, query 2.96 (1.03×)
frequency-moments__univmon-l2__r8-10hz-24h-every10s-releps0.2-d0.1 UnivMon 624×5, query 106.4 (5.91×) UnivMon 624×5, ingestion 301 (2.09×)
frequency-moments__univmon-l2__r8-10hz-5m-every10s-releps0.1-d0.1 UnivMon 624×5, query 0.3722 (3.38×) UnivMon 624×5, ingestion 1.131 (1.11×)
frequency-moments__univmon-l2__r8-1hz-24h-every60s-releps0.1-d0.1 UnivMon 624×5, query 1.95 (4.54×) UnivMon 624×5, query 7.273 (1.22×)
heavy-hitters__cmsheap-topk10__u1000-i1-10hz-1h-every60s-l1eps0.01-d0.05 CMS+heap 272×10, query 2.463 (1.02×) exact 2.505 (1.00×)
heavy-hitters__cmsheap-topk10__u1000-i1-10hz-24h-every300s-l1eps0.002-d0.01 CMS+heap 1360×12, query 6.481 (1.00×) exact 6.49 (1.00×)
heavy-hitters__cmsheap-topk10__u1000-i1-1hz-5m-every10s-l1eps0.01-d0.05 CMS+heap 272×10, query 2.281 (1.11×) exact 2.534 (1.00×)
heavy-hitters__cmsheap-topk10__u1000-i1-1hz-5m-every60s-l1eps0.01-d0.05 CMS+heap 272×10, query 0.3337 (1.13×) exact 0.3759 (1.00×)
heavy-hitters__cmsheap-topk10__u1000-i2-1hz-1h-every60s-l1eps0.01-d0.05 CMS+heap 272×12, query 3.31 (1.01×) exact 3.348 (1.00×)
heavy-hitters__cmsheap-topk10__u2500-i1-10hz-5m-every10s-l1eps0.002-d0.05 CMS+heap 1360×12, query 7.731 (1.08×) exact 8.355 (1.00×)
heavy-hitters__cmsheap-topk10__u2500-i1-1hz-1h-every60s-l1eps0.01-d0.05 CMS+heap 272×12, query 4.138 (1.03×) exact 4.242 (1.00×)
heavy-hitters__cmsheap-topk10__u2500-i2-10hz-5m-every10s-l1eps0.01-d0.05 CMS+heap 272×12, query 15.46 (1.04×) exact 16.03 (1.00×)
heavy-hitters__cmsheap-topk10__u2500-i2-1hz-24h-every60s-l1eps0.002-d0.05 CMS+heap 1360×12, query 174 (1.00×) exact 174.1 (1.00×)
heavy-hitters__cmsheap-topk10__u5000-i1-10hz-1h-every10s-l1eps0.01-d0.05 CMS+heap 272×12, query 104.7 (1.01×) exact 106 (1.00×)
heavy-hitters__cmsheap-topk10__u5000-i1-1hz-24h-every300s-l1eps0.002-d0.05 CMS+heap 1360×12, query 28.37 (1.00×) exact 28.41 (1.00×)
heavy-hitters__cmsheap-topk10__u5000-i1-1hz-5m-every60s-l1eps0.01-d0.05 CMS+heap 272×12, query 1.671 (1.12×) exact 1.879 (1.00×)
heavy-hitters__csheap-topk10__u100-i1-10hz-5m-every10s-l2eps0.05-d0.1 CS+heap 1200×81, query 0.322 (1.04×) exact 0.3345 (1.00×)
heavy-hitters__csheap-topk10__u100-i1-1hz-1h-every60s-l2eps0.02-d0.1 CS+heap 7500×81, query 0.1677 (1.01×) exact 0.1697 (1.00×)
heavy-hitters__csheap-topk10__u100-i1-1hz-5m-every60s-l2eps0.1-d0.1 CS+heap 300×81, query 0.03556 (1.06×) exact 0.03763 (1.00×)
heavy-hitters__csheap-topk10__u1000-i1-1hz-5m-every60s-l2eps0.05-d0.1 CS+heap 1200×117, query 0.3658 (1.03×) exact 0.3759 (1.00×)
heavy-hitters__csheap-topk10__u300-i1-10hz-1h-every60s-l2eps0.1-d0.1 CS+heap 300×117, query 0.7485 (1.00×) exact 0.7515 (1.00×)
heavy-hitters__csheap-topk10__u300-i1-1hz-5m-every60s-l2eps0.05-d0.1 CS+heap 1200×117, query 0.1098 (1.03×) exact 0.1128 (1.00×)
mixed__edge-dashboard__u1000-i1-1hz-r4-10hz UnivMon 624×5, query; CMS+heap 272×10, query 22.83 (2.84×) UnivMon 624×5, ingestion 33.67 (1.92×)
mixed__l2-24h-and-15m__r1-10hz-every60s UnivMon 624×5, query; UnivMon 624×5, query 2.239 (4.88×) UnivMon 624×5, ingestion 4.919 (2.22×)
mixed__l2-24h-and-15m__r4-10hz-every60s UnivMon 624×5, query; UnivMon 624×5, query 8.958 (5.70×) UnivMon 624×5, ingestion 19.67 (2.60×)
mixed__l2-24h-and-15m__r8-10hz-every60s UnivMon 624×5, query; UnivMon 624×5, query 17.92 (5.89×) UnivMon 624×5, ingestion 39.35 (2.68×)
mixed__l2-and-count__r1-10hz-24h-every60s UnivMon 624×5, query 2.246 (4.84×) UnivMon 624×5, ingestion 4.945 (2.20×)
mixed__l2-and-count__r4-10hz-24h-every60s UnivMon 624×5, query 8.983 (5.66×) UnivMon 624×5, ingestion 19.78 (2.57×)
mixed__l2-and-count__r8-10hz-24h-every60s UnivMon 624×5, query 17.97 (5.85×) UnivMon 624×5, ingestion 39.56 (2.66×)
mixed__l2-and-distinct__r1-10hz-24h-every60s UnivMon 624×5, query; KMV k=4323, ingestion 4.573 (4.70×) UnivMon 624×5, ingestion; KMV k=4323, ingestion 7.273 (2.95×)
mixed__l2-and-distinct__r4-10hz-24h-every60s UnivMon 624×5, query; KMV k=4323, ingestion 18.29 (5.50×) UnivMon 624×5, ingestion; KMV k=4323, ingestion 29.09 (3.46×)
mixed__l2-and-distinct__r8-10hz-24h-every60s UnivMon 624×5, query; KMV k=4323, ingestion 36.58 (5.69×) UnivMon 624×5, ingestion; KMV k=4323, ingestion 58.18 (3.57×)
mixed__repeat-rate-and-l2__r1-10hz-24h-every60s UnivMon 2367×5, query 2.246 (4.84×) UnivMon 2367×5, query 8.899 (1.22×)
mixed__repeat-rate-and-l2__r4-10hz-24h-every60s UnivMon 2367×5, query 8.983 (5.66×) UnivMon 2367×5, query 35.6 (1.43×)
mixed__topk-and-total__u1000-i1-1hz-1h-every60s CMS+heap 272×10, query 1.662 (1.02×) exact 1.697 (1.00×)
mixed__topk-and-total__u5000-i1-1hz-1h-every60s CMS+heap 272×12, query 8.312 (1.02×) exact 8.485 (1.00×)
mixed__topk-by-url-and-by-instance__u100-i10-1hz-1h-every60s CMS+heap 272×10, query 3.306 (1.00×) exact 3.307 (1.00×)
mixed__topk-by-url-and-by-instance__u500-i10-1hz-1h-every60s CMS+heap 272×12, query 16.53 (1.00×) exact 16.54 (1.00×)
mixed__topk-dashboard-and-alert__u1000-i1-1hz-1h-every10s-and-60s CMS+heap 1360×10, query; CMS+heap 272×10, query 22.36 (1.01×) exact 22.66 (1.00×)
mixed__topk-dashboard-and-alert__u5000-i1-1hz-1h-every10s-and-60s CMS+heap 1360×12, query; CMS+heap 272×12, query 111.8 (1.01×) exact 113.3 (1.00×)

Follow-ups

  • Executor heap fix, then re-measure the heap constants. The weighted-frequency kernel's per-row rescore_heap() makes each row cost (heap size + 1) estimates. Offering only the touched item (asap_sketchlib's own try_update_weighted on the heap sketch) keeps every certified guarantee and would cut Count-Min with heap from 3,608 to 596 ns per row at depth 10, and Count Sketch with heap at depth 81 from 29.0 to 6.6–9.1 µs. After it, CMS_HEAP_OPERATIONS_PER_ESTIMATE, CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER, COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATE and COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER must be measured again.

  • Exact top-k is over-charged about 2.2–3.8× ([Bug] Exact top-k is charged 2.2–3.8× its measured time; with the executor heap fix the planner would pick a slower sketch #1386). It must be recalibrated together with the executor heap fix. Otherwise the cheaper heap would make the model pick the sketch where exact is still 1.9–15× faster.

  • PromQL top-k items get no distinct count ([Bug] A top-k heap over a PromQL item (tuple or series identity) is costed as if every row's item differed #1387).

  • The 19 µs per-execution readout constant, and an allocation model that follows the allocator's regimes, if large states matter.

  • Hydra and HyperLogLog are still charged unmeasured constants; plain Count Sketch has no executor build path to measure.

  • The UnivMon benefits now rest on its measured 95 per counter, a cost the hashing in asap_sketchlib sets: hashing every row separately for each of its sketch rows. Sharing one hash across the rows would lower it; that belongs in a separate sketchlib PR.

Human review — do not complete with an agent

  • The MVP boundary is correct.
  • New conceptual layers or public interfaces are necessary.
  • The before/after description matches the intended product behavior.
  • Human reviewer:
  • Decision and rationale:

…-estimates, readout and merge, measured on the executor

Count-Min and Count Sketch (with and without a top-k heap) and UnivMon
get constants fitted on the executor's builds, readouts and merges: a
counter update per row of depth, the heap kernel's re-estimate of every
resident per row, each state's counters allocated and zeroed when it is
built, a merge per counter plus the heap residents re-estimated, and a
heap top-k read per resident counter. Each is a CostConstant.

A heap holds the distinct items its state folded, at most its size: the
build reads StateInputRows::distinct_values from the summary's item
column (a keyed summary), else its weight column. StateLastWrittenBy
carries the state's input rows so the readout counts the residents.
…op-k now costs less than a query-time Count-Min heap

The Count-Min Sketch with heap the snapshot selected is charged its
measured heap re-estimates per row (the executor's kernel re-estimates
every resident after each update), so the exact hash aggregate over the
same 10,000 rows is now the cheaper plan.
@zzylol
zzylol force-pushed the fix/query-time-sketch-size-cost branch from 8188722 to 6004805 Compare November 17, 2026 18:58
@zzylol
zzylol changed the base branch from fix/kmv-build-cost-refit to main November 17, 2026 18:58
…ct items; review fixes

StateLastWrittenBy::Merge carries the merged state's input rows, so a top-k
heap read after a merge counts the items it holds instead of a full heap.
The KMV merge reuses distinct_items_seen; counters_updated_per_row no
longer has an arm for the heap families, whose work is charged apart. The
new CostConstant variants follow the constants' declaration order. A
SummaryAgg's detail names the allocation only when there is one. Docs:
held-out ingestion range, the hand-off in the heap example, the input
state's residents in the merge, the allocation example's charged
difference. The snapshot manifest says the top-k rate snapshot now plans
exact.
@zzylol
zzylol merged commit 82f70ca into main Nov 17, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cost-model Cost formulas, statistics, ranking, and selection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] The cost model does not charge for a sketch's width: a query-time Count Sketch costs the same at 24,300 or 607,500 counters

1 participant