Add index to event_type and entity_id (#89676)

This commit is contained in:
J. Nick Koston 2023-03-13 23:09:21 -10:00 committed by GitHub
parent b620e5d8a6
commit dbc0890ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -68,7 +68,7 @@ class Base(DeclarativeBase):
"""Base class for tables."""
SCHEMA_VERSION = 40
SCHEMA_VERSION = 41
_LOGGER = logging.getLogger(__name__)
@ -348,7 +348,9 @@ class EventTypes(Base):
__table_args__ = (_DEFAULT_TABLE_ARGS,)
__tablename__ = TABLE_EVENT_TYPES
event_type_id: Mapped[int] = mapped_column(Integer, Identity(), primary_key=True)
event_type: Mapped[str | None] = mapped_column(String(MAX_LENGTH_EVENT_EVENT_TYPE))
event_type: Mapped[str | None] = mapped_column(
String(MAX_LENGTH_EVENT_EVENT_TYPE), index=True
)
def __repr__(self) -> str:
"""Return string representation of instance for debugging."""
@ -597,7 +599,9 @@ class StatesMeta(Base):
__table_args__ = (_DEFAULT_TABLE_ARGS,)
__tablename__ = TABLE_STATES_META
metadata_id: Mapped[int] = mapped_column(Integer, Identity(), primary_key=True)
entity_id: Mapped[str | None] = mapped_column(String(MAX_LENGTH_STATE_ENTITY_ID))
entity_id: Mapped[str | None] = mapped_column(
String(MAX_LENGTH_STATE_ENTITY_ID), index=True
)
def __repr__(self) -> str:
"""Return string representation of instance for debugging."""

View File

@ -1054,6 +1054,9 @@ def _apply_update( # noqa: C901
"statistics_short_term",
"ix_statistics_short_term_metadata_id",
)
elif new_version == 41:
_create_index(session_maker, "event_types", "ix_event_types_event_type")
_create_index(session_maker, "states_meta", "ix_states_meta_entity_id")
else:
raise ValueError(f"No schema migration defined for version {new_version}")