From d1ee303e8547396ef220f5229aa56b6963af07ab Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 12 Mar 2023 22:24:57 -1000 Subject: [PATCH] Drop duplicated indices from recorder database schema (#89613) Drop duplicated indices from schema https://docs.percona.com/percona-toolkit/pt-duplicate-key-checker.html ``` % pt-duplicate-key-checker --databases fresh ALTER TABLE `fresh`.`events` DROP INDEX `ix_events_event_type_id`; ALTER TABLE `fresh`.`states` DROP INDEX `ix_states_metadata_id`; ALTER TABLE `fresh`.`statistics` DROP INDEX `ix_statistics_metadata_id`; ALTER TABLE `fresh`.`statistics_short_term` DROP INDEX `ix_statistics_short_term_metadata_id`; ``` --- homeassistant/components/recorder/db_schema.py | 7 +++---- homeassistant/components/recorder/migration.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/recorder/db_schema.py b/homeassistant/components/recorder/db_schema.py index 5e0a78dad3b..b715ef9bc58 100644 --- a/homeassistant/components/recorder/db_schema.py +++ b/homeassistant/components/recorder/db_schema.py @@ -68,7 +68,7 @@ class Base(DeclarativeBase): """Base class for tables.""" -SCHEMA_VERSION = 39 +SCHEMA_VERSION = 40 _LOGGER = logging.getLogger(__name__) @@ -229,7 +229,7 @@ class Events(Base): LargeBinary(CONTEXT_ID_BIN_MAX_LENGTH) ) event_type_id: Mapped[int | None] = mapped_column( - Integer, ForeignKey("event_types.event_type_id"), index=True + Integer, ForeignKey("event_types.event_type_id") ) event_data_rel: Mapped[EventData | None] = relationship("EventData") event_type_rel: Mapped[EventTypes | None] = relationship("EventTypes") @@ -426,7 +426,7 @@ class States(Base): LargeBinary(CONTEXT_ID_BIN_MAX_LENGTH) ) metadata_id: Mapped[int | None] = mapped_column( - Integer, ForeignKey("states_meta.metadata_id"), index=True + Integer, ForeignKey("states_meta.metadata_id") ) states_meta_rel: Mapped[StatesMeta | None] = relationship("StatesMeta") @@ -617,7 +617,6 @@ class StatisticsBase: metadata_id: Mapped[int | None] = mapped_column( Integer, ForeignKey(f"{TABLE_STATISTICS_META}.id", ondelete="CASCADE"), - index=True, ) start: Mapped[datetime | None] = mapped_column( DATETIME_TYPE, index=True diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index b1b33dd29e2..5b2180773a3 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -1041,6 +1041,19 @@ def _apply_update( # noqa: C901 "ix_statistics_short_term_statistic_id_start", quiet=True, ) + elif new_version == 40: + # ix_events_event_type_id is a left-prefix of ix_events_event_type_id_time_fired_ts + _drop_index(session_maker, "events", "ix_events_event_type_id") + # ix_states_metadata_id is a left-prefix of ix_states_metadata_id_last_updated_ts + _drop_index(session_maker, "states", "ix_states_metadata_id") + # ix_statistics_metadata_id is a left-prefix of ix_statistics_statistic_id_start_ts + _drop_index(session_maker, "statistics", "ix_statistics_metadata_id") + # ix_statistics_short_term_metadata_id is a left-prefix of ix_statistics_short_term_statistic_id_start_ts + _drop_index( + session_maker, + "statistics_short_term", + "ix_statistics_short_term_metadata_id", + ) else: raise ValueError(f"No schema migration defined for version {new_version}")