perf(objective_lots): inline DISTINCT ON for 3 view-fullscan consumers + cache landing stats (#1953) #2067

Merged
bot-backend merged 2 commits from fix/perf-view-fullscan-1953 into main 2026-06-28 17:25:42 +00:00

2 commits

Author SHA1 Message Date
c16e071859 test(supply_layers): assert inline DISTINCT ON physflat-dedup, not the view
All checks were successful
CI / changes (pull_request) Successful in 7s
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Successful in 1m57s
CI / backend-tests (pull_request) Successful in 14m14s
Perf-батч #2 заменил _L1_OPEN_SQL view→inline DISTINCT ON. Тест из #1964
(test_sql_reads_physflat_view) ассертил наличие view + отсутствие FROM
objective_lots — теперь инвертирован под inline. Новый инвариант сохраняет
анти-регресс #1964 (требует physflat-дедуп через DISTINCT ON, иначе сырой
objective_lots раздул бы units_available ~2.91×) и запрещает view (full-scan).
2026-06-28 22:10:44 +05:00
e33273591b perf(objective_lots): inline DISTINCT ON for 3 view-fullscan consumers + cache landing stats (#1953)
Some checks failed
CI / changes (pull_request) Successful in 8s
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Successful in 2m10s
CI / backend-tests (pull_request) Failing after 14m34s
#1964 repointed landing/supply_layers/special_indices to v_objective_lots_latest,
whose DISTINCT ON is an optimization barrier — outer WHERE can't push below it, so
the planner full Parallel-Seq-Scans 1.76M rows + external merge sorts (~6-8s) per call.

- landing._query_stats: add 1h in-process TTL cache (homepage /landing/stats was
  uncached; KPI3 full-view-aggregate measured 6.7s/load). Threadpool-aware lock,
  DB query outside lock, only successful result cached. Mirrors weather_cache.
- supply_layers._L1_OPEN_SQL: view->inline DISTINCT ON, stable filters
  (premise_kind/district) pushed below dedup -> Bitmap on district_class_idx.
  district-scoped 5.9s->0.45s (~13x).
- special_indices._ARTIFICIAL_DEMAND_SQL: view->inline 2-layer CTE; stable filters
  before dedup, changing filters (is_sold/contract_date/class) AFTER dedup.
  class has 248 transient physflats -> before-dedup drifted +193 on standard.
  district-scoped 6.7s->0.57s (~12x), EKB-wide 8.1s->5.5s.

Result bit-identical to view, verified on prod across all classes/districts.
Uses existing covering / district_class indexes (no migration).
2026-06-28 21:41:35 +05:00