From 339a839dbe8cae07a84f0c541454487e16acbdb8 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sat, 3 Mar 2018 22:54:38 +0100 Subject: [PATCH] Add SQL index to states.event_id (#12825) --- homeassistant/components/recorder/migration.py | 3 +++ homeassistant/components/recorder/models.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 325267b857e..af70c9d998c 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -143,6 +143,9 @@ def _apply_update(engine, new_version, old_version): _drop_index(engine, "states", "ix_states_entity_id_created") _create_index(engine, "states", "ix_states_entity_id_last_updated") + elif new_version == 5: + # Create supporting index for States.event_id foreign key + _create_index(engine, "states", "ix_states_event_id") else: raise ValueError("No schema migration defined for version {}" .format(new_version)) diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 7c29c8045ea..32d6291b90c 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -16,7 +16,7 @@ from homeassistant.remote import JSONEncoder # pylint: disable=invalid-name Base = declarative_base() -SCHEMA_VERSION = 4 +SCHEMA_VERSION = 5 _LOGGER = logging.getLogger(__name__) @@ -64,7 +64,7 @@ class States(Base): # type: ignore entity_id = Column(String(255)) state = Column(String(255)) attributes = Column(Text) - event_id = Column(Integer, ForeignKey('events.event_id')) + event_id = Column(Integer, ForeignKey('events.event_id'), index=True) last_changed = Column(DateTime(timezone=True), default=datetime.utcnow) last_updated = Column(DateTime(timezone=True), default=datetime.utcnow, index=True)