From da44f80b32da3f10c7a6daff639d86f3436c5cf3 Mon Sep 17 00:00:00 2001 From: Mike Megally Date: Sat, 1 Jul 2017 14:10:17 -0700 Subject: [PATCH] Create an index on the states table to help hass startup time (#8255) --- homeassistant/components/recorder/migration.py | 2 ++ homeassistant/components/recorder/models.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 0fac32bdec7..043887b7cab 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -64,6 +64,8 @@ def _apply_update(engine, new_version): # Create indexes for states _create_index(engine, "states", "ix_states_last_updated") _create_index(engine, "states", "ix_states_entity_id_created") + elif new_version == 3: + _create_index(engine, "states", "ix_states_created_domain") 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 6dcb5dbd051..65c7dc6abb6 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 = 2 +SCHEMA_VERSION = 3 _LOGGER = logging.getLogger(__name__) @@ -75,7 +75,9 @@ class States(Base): # type: ignore Index('states__significant_changes', 'domain', 'last_updated', 'entity_id'), Index('ix_states_entity_id_created', - 'entity_id', 'created'),) + 'entity_id', 'created'), + Index('ix_states_created_domain', + 'created', 'domain'),) @staticmethod def from_event(event):