Repository navigation
Description
Originated in the review of #1789 (fluree-db-stats kernel and on-demand column profiling), which asked whether the new sketches could serve query planning. Nothing in that PR runs at index time; this issue is the follow-up that would.
Problem
The planner reasons from count, ndv_values and ndv_subjects per (graph, property), and two estimates are known to be wrong in a way those three cannot fix:
- Bound object is
count / ndv_values, a uniform assumption (fluree-db-query/src/planner.rs,estimate_triple_row_count,PatternType::BoundObject).hash_join.rsdocuments the BSBM case:country=USis estimated at ~28K rows against an actual 61.8K, andHASH_JOIN_MAX_SCAN_RATIOwas loosened to 1024× so the join this operator exists for is not rejected on that error. - FILTER selectivity is a fixed 50% (
fluree-db-query/src/filter.rs). BSBM Q1, Q3 and Q5 carry numeric range filters.
Proposal
Persist two more sketches per (graph, property) in the indexer's HllSketchBlob, computed in the same IdStatsHook::apply_record pass that feeds the HLL today:
- Most common values:
fluree_db_stats::HeavyHitters(Misra–Gries, 64 counters) keyed on theo_hashthe hook already computes. For the planner the display sample is unnecessary; a(hash, count)pair per counter is enough. Consumption: hash the constant object the same way, use the counter when present, otherwise(count − Σ mcv) / (ndv − |mcv|), the Postgres formula. Exact for low-cardinality properties, bounded for popular values elsewhere. - Value distribution:
fluree_db_stats::TDigestper numeric and temporal property. Consumption:cdf(K)forFILTER(?v > K)and the like; min/max for a zero-result short-circuit.
Both merge, so the incremental refresh path folds novelty sketches into the persisted ones the way it folds the HLL. Both are insert-only, which matches the HLL's existing semantics: retractions do not decrement, a full rebuild resets. For planning that means stale-high under deletes, which is conservative.
Constraints
- Bench first. The MCV adds a hash-map lookup per flake plus an O(64) sweep when the table is full and the value is unseen; the digest adds a buffered push and a sort of ~600 every 500 values. That roughly doubles the stats hook's per-flake work, and the hook has stalled imports before. Gate it behind a flag and measure on the DBLP import before it is on by default.
- Persistence is a
HllSketchBlobversion bump withserde(default)on the new fields; a few kilobytes per property. assemble_fast_statsinfluree-db-noveltymerges novelty deltas into the counts at query time; the MCV can be left base-only within the novelty window.
Acceptance
- The
hash_join_cost_winsBI-1 case estimatescountry=USwithin the sketch's error bar, and the 1024× cap can be tightened back toward the measured break-even. - A FILTER-selectivity test on a skewed numeric property where the 50% constant misorders a two-pattern join and the digest does not.
- Import bench shows the per-flake cost of the hook with the flag on, against the flag off.
Activity