-- =============================================================
-- V066__latest_telemetry_distinct_on_indexes.sql
-- Zrychlení view ems.vw_latest_* (PostgREST dashboard endpoints).
--
-- View používají DISTINCT ON (...) s ORDER BY ... measured_at desc.
-- Bez odpovídajících indexů může plán spadnout na scan+sort nad
-- velkými Timescale hypertabulkami (sekundy latency).
-- =============================================================

create index if not exists idx_telemetry_inverter_site_inverter_time_desc
  on ems.telemetry_inverter (site_id, inverter_id, measured_at desc);

create index if not exists idx_telemetry_ev_site_charger_connector_time_desc
  on ems.telemetry_ev_charger (site_id, charger_id, connector_id, measured_at desc);

create index if not exists idx_telemetry_hp_site_heat_pump_time_desc
  on ems.telemetry_heat_pump (site_id, heat_pump_id, measured_at desc);

