-- Zrychlení fn_pv_forecast_delta_profile (volá ho pv-slots-corrected): range scan site + interval_start
-- s podmínkami učení bez sekvenčního full scanu větší historie.

create index if not exists idx_forecast_accuracy_site_interval_delta_profile
  on ems.forecast_accuracy (
    site_id,
    interval_start desc,
    pv_array_id,
    forecast_created_at desc
  )
  where actual_power_w is not null
    and coalesce(learning_eligible, true) = true
    and forecast_created_at <= interval_start;

comment on index ems.idx_forecast_accuracy_site_interval_delta_profile is
  'Partial index pro výběr posledního forecast runu na slot (DISTINCT ON interval_start, pv_array_id) v delta profilu.';
