Analysis: bound the freshness query, let window sums skip chunks, share the Overview's catalog
ci / build-test (push) Successful in 2m34s
ci / build-test (push) Successful in 2m34s
Three things the 1,000-meter x 10-year measurement found, each proved by EXPLAIN or a statement count before and after. No tally moves. Freshness had two jobs in one unbounded query. The mark -- when a meter last delivered -- is now stored on meter_rollup_state and maintained by every recompute, with a one-pass backfill in the migration, so an import-only meter keeps its years-old last activity without reading a single raw row. The rhythm that decides stale versus live is sampled inside a 90-day window and only for meters that actually have a live source; a source silent for longer than that is re-read unbounded, so it is still called stale by its own rhythm rather than by a default. The portfolio query went from 13.8 ms planning plus 36.1 ms execution across all 123 reading chunks to 0.58 plus 0.44 ms across four. Window sums took their time bounds only from the unnest join, so the planner could not exclude chunks: a 1,960-window case scanned 1.39 M rows in parallel and spilled a 45 MB sort. Repeating the overall min and max as constants makes it five chunks and nested-loop index scans, 121.5 ms to 8.7 ms. The Overview read the catalog three times, once for the quantities and once for each of its two bills. One context and one catalog snapshot now feed all three: 31 statements per load to 23. The final timings on an idle machine are in docs/ANALYSIS_REPORT.md: the brief's target request (100 meters, ten years, monthly) is 286 ms against two seconds, and a startup rebuild of 1,000 meters is 279 s.
This commit is contained in:
@@ -42,6 +42,9 @@ internal sealed class BillRun
|
||||
private readonly Dictionary<int, DateOnly?> _firstData = [];
|
||||
private readonly List<AnalysisProblem> _quantityProblems = [];
|
||||
|
||||
/// <summary>A catalog the caller already loaded and wants this run to reuse (A-40); null to load one.</summary>
|
||||
private readonly AnalysisCatalog? _sharedCatalog;
|
||||
|
||||
private AnalysisCatalog _catalog = null!;
|
||||
private TariffBook _book = null!;
|
||||
private TotalsClassification _full = null!;
|
||||
@@ -59,7 +62,8 @@ internal sealed class BillRun
|
||||
private Dictionary<int, IReadOnlyList<BucketValue>> _periodSpans = [];
|
||||
private Dictionary<int, AnalysisSeries> _series = [];
|
||||
|
||||
public BillRun(MeterVaultDbContext db, AnalysisReader reader, CostAnalysisRequest request, string currency)
|
||||
public BillRun(
|
||||
MeterVaultDbContext db, AnalysisReader reader, CostAnalysisRequest request, string currency, AnalysisCatalog? catalog = null)
|
||||
{
|
||||
_db = db;
|
||||
_reader = reader;
|
||||
@@ -68,6 +72,7 @@ internal sealed class BillRun
|
||||
_zone = reader.Zone;
|
||||
_currency = currency;
|
||||
_today = PeriodResolver.LocalDate(_period.Now, _zone);
|
||||
_sharedCatalog = catalog;
|
||||
}
|
||||
|
||||
private bool WantsCategories =>
|
||||
@@ -120,7 +125,10 @@ internal sealed class BillRun
|
||||
|
||||
private async Task LoadAsync(bool categories, CancellationToken cancellationToken)
|
||||
{
|
||||
_catalog = await AnalysisCatalog.LoadAsync(_db, _zone, cancellationToken).ConfigureAwait(false);
|
||||
// The catalog is a snapshot of meters, tanks, links and rollup states; a caller that reads several figures of
|
||||
// one page from it (the Overview's bill and its comparison) passes the one it already holds, so they answer
|
||||
// from the same snapshot and the load is not repeated (A-40).
|
||||
_catalog = _sharedCatalog ?? await AnalysisCatalog.LoadAsync(_db, _zone, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// One tariff load per request; ordered so that the book's deterministic tie-break never depends on the plan.
|
||||
var tariffs = await _db.Tariffs.AsNoTracking().OrderBy(t => t.Id).ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Reference in New Issue
Block a user