Reduce size of load query to prime event_types and states_meta at startup (#89677)
This commit is contained in:
parent
c2c809682a
commit
0630b7b962
3 changed files with 25 additions and 17 deletions
|
@ -793,13 +793,13 @@ class Recorder(threading.Thread):
|
|||
until its primed.
|
||||
"""
|
||||
assert self.event_session is not None
|
||||
if hashes := [
|
||||
if hashes := {
|
||||
StateAttributes.hash_shared_attrs_bytes(shared_attrs_bytes)
|
||||
for event in events
|
||||
if (
|
||||
shared_attrs_bytes := self._serialize_state_attributes_from_event(event)
|
||||
)
|
||||
]:
|
||||
}:
|
||||
with self.event_session.no_autoflush:
|
||||
for hash_chunk in chunked(hashes, SQLITE_MAX_BIND_VARS):
|
||||
for id_, shared_attrs in self.event_session.execute(
|
||||
|
@ -815,11 +815,11 @@ class Recorder(threading.Thread):
|
|||
the data in the database for every event until its primed.
|
||||
"""
|
||||
assert self.event_session is not None
|
||||
if hashes := [
|
||||
if hashes := {
|
||||
EventData.hash_shared_data_bytes(shared_event_bytes)
|
||||
for event in events
|
||||
if (shared_event_bytes := self._serialize_event_data_from_event(event))
|
||||
]:
|
||||
}:
|
||||
with self.event_session.no_autoflush:
|
||||
for hash_chunk in chunked(hashes, SQLITE_MAX_BIND_VARS):
|
||||
for id_, shared_data in self.event_session.execute(
|
||||
|
|
|
@ -9,8 +9,10 @@ from sqlalchemy.orm.session import Session
|
|||
|
||||
from homeassistant.core import Event
|
||||
|
||||
from ..const import SQLITE_MAX_BIND_VARS
|
||||
from ..db_schema import EventTypes
|
||||
from ..queries import find_event_type_ids
|
||||
from ..util import chunked
|
||||
|
||||
CACHE_SIZE = 2048
|
||||
|
||||
|
@ -27,7 +29,7 @@ class EventTypeManager:
|
|||
def load(self, events: list[Event], session: Session) -> None:
|
||||
"""Load the event_type to event_type_ids mapping into memory."""
|
||||
self.get_many(
|
||||
(event.event_type for event in events if event.event_type is not None),
|
||||
{event.event_type for event in events if event.event_type is not None},
|
||||
session,
|
||||
)
|
||||
|
||||
|
@ -51,8 +53,9 @@ class EventTypeManager:
|
|||
return results
|
||||
|
||||
with session.no_autoflush:
|
||||
for missing_chunk in chunked(missing, SQLITE_MAX_BIND_VARS):
|
||||
for event_type_id, event_type in session.execute(
|
||||
find_event_type_ids(missing)
|
||||
find_event_type_ids(missing_chunk)
|
||||
):
|
||||
results[event_type] = self._id_map[event_type] = cast(
|
||||
int, event_type_id
|
||||
|
|
|
@ -9,8 +9,10 @@ from sqlalchemy.orm.session import Session
|
|||
|
||||
from homeassistant.core import Event
|
||||
|
||||
from ..const import SQLITE_MAX_BIND_VARS
|
||||
from ..db_schema import StatesMeta
|
||||
from ..queries import find_all_states_metadata_ids, find_states_metadata_ids
|
||||
from ..util import chunked
|
||||
|
||||
CACHE_SIZE = 8192
|
||||
|
||||
|
@ -27,11 +29,11 @@ class StatesMetaManager:
|
|||
def load(self, events: list[Event], session: Session) -> None:
|
||||
"""Load the entity_id to metadata_id mapping into memory."""
|
||||
self.get_many(
|
||||
(
|
||||
{
|
||||
event.data["new_state"].entity_id
|
||||
for event in events
|
||||
if event.data.get("new_state") is not None
|
||||
),
|
||||
},
|
||||
session,
|
||||
)
|
||||
|
||||
|
@ -60,10 +62,13 @@ class StatesMetaManager:
|
|||
return results
|
||||
|
||||
with session.no_autoflush:
|
||||
for missing_chunk in chunked(missing, SQLITE_MAX_BIND_VARS):
|
||||
for metadata_id, entity_id in session.execute(
|
||||
find_states_metadata_ids(missing)
|
||||
find_states_metadata_ids(missing_chunk)
|
||||
):
|
||||
results[entity_id] = self._id_map[entity_id] = cast(int, metadata_id)
|
||||
results[entity_id] = self._id_map[entity_id] = cast(
|
||||
int, metadata_id
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue