Skip to content

Default unit costs: shared-scan instant reads, carried labels and small sketches are still misranked #1359

Open

Description

@zzylol

Problem, by example

count by (job) (m) and sum by (job) (count_over_time(m[1m])), both every 60 s, over 1,000 gauge series m sampled every 1 s (arriving at 1,000 rows/s, raw data kept, loose absolute targets), with the default CostInputs after #1355:

Plan Shape Predicted cost/s Measured query CPU per evaluation (p50)
Exact query-time lowering two scans: Scan(1 s) → TimeRange(1s, instant) → Aggregate by (job); Scan(60 s) → TimeRange(60s) → per-series Count → Aggregate by (job) 0.04600 17.9 ms
Selected one shared Scan(60 s): TimeRange(60s) → per-series Count → Aggregate by (job), and TimeRange(1s, instant) → Aggregate by (job) over the same scan 0.04590 20.7 ms (0.87× of exact)

Stage 3 predicts the two plans equal, but the selected plan measures 13% slower. (Measured at #1355's head 008704da2, release, one pinned core, 600 s of evaluations; on main the selected plan measured 0.84×.)

Two more gaps of the same kind, from #1355's measurements:

  • A hash aggregate that carries an extra label per row: sum by (job) (sum_over_time(latency[1m])) at 1 Hz, every 10 s. The exact per-series aggregate measures 68 ns/row with a job label and 27 without, but is charged 27. It accounts for most of the remaining misranks in fix(cost): charge each operation the executor's measured time, so a sketch plan is no longer predicted cheaper than a faster exact plan #1355's 85-case grid (exact sum_over_time / count_over_time with sum by (job) over 1m and 5m windows).
  • Small query-time sketches: a mixed workload with 5m windows (300 rows per series) selects exact quantiles measured at 126.0 ms against 117.6 ms for main's query-time sketch plan (still 1.03× faster than exact): the DDSketch update and merge constants, measured on 3,600-row states, over-charge small ones a little.

Why it happens

  • The instant TimeRange over the shared 60 s scan reads 60,000 rows to keep 1,000, but estimate_operator charges a time range by the rows it keeps (output.rows × TIME_RANGE_OPERATIONS_PER_ROW, crates/cost/src/node/analytical.rs), so the shared scan looks free for the instant reader. This is the likely cause; it has not been measured per node.
  • hash_aggregate_operations_per_row (crates/cost/src/node/operations_per_unit.rs) charges 15 per grouping key and 12 per measure, and nothing for the columns the group carries.
  • The DDSketch and KLL constants are single numbers per row or state, not a function of the state's rows.

Proposal

Scope and acceptance

  • Acceptance: default_cost_plan_ranking::predicted_cpu_orders_exact_and_alternative_plans_as_measured gains the three cases above (with the measured numbers) and passes.

Related

Activity

  1. added
    cost-model
    enhancement
    on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

cost-modelenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions