diff --git a/homeassistant/components/recorder/db_schema.py b/homeassistant/components/recorder/db_schema.py index 263a8b781d3..6e7230e4023 100644 --- a/homeassistant/components/recorder/db_schema.py +++ b/homeassistant/components/recorder/db_schema.py @@ -181,7 +181,9 @@ class Events(Base): __table_args__ = ( # Used for fetching events at a specific time # see logbook - Index("ix_events_event_type_time_fired_ts", "event_type", "time_fired_ts"), + Index( + "ix_events_event_type_id_time_fired_ts", "event_type_id", "time_fired_ts" + ), Index( EVENTS_CONTEXT_ID_BIN_INDEX, "context_id_bin", diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 5f81af68654..4022c6df053 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -983,6 +983,7 @@ def _apply_update( # noqa: C901 elif new_version == 37: _add_columns(session_maker, "events", [f"event_type_id {big_int}"]) _create_index(session_maker, "events", "ix_events_event_type_id") + _create_index(session_maker, "events", "ix_events_event_type_id_timed_fired_ts") else: raise ValueError(f"No schema migration defined for version {new_version}") @@ -1337,6 +1338,9 @@ def migrate_event_type_ids(instance: Recorder) -> bool: if is_done: _drop_index(session_maker, "events", "ix_events_event_type", quiet=True) + _drop_index( + session_maker, "events", "ix_events_event_type_timed_fired_ts", quiet=True + ) _LOGGER.debug("Migrating event_types done=%s", is_done) return is_done