Avoid buffering logbook rows unless we are selecting less than a days worth (#71809)

pull/71821/head
J. Nick Koston 2022-05-13 12:10:43 -04:00 committed by GitHub
parent e7e45209ec
commit d215cdc563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -445,10 +445,22 @@ def _get_events(
def yield_rows(query: Query) -> Generator[Row, None, None]: def yield_rows(query: Query) -> Generator[Row, None, None]:
"""Yield Events that are not filtered away.""" """Yield Events that are not filtered away."""
if entity_ids or context_id: # end_day - start_day intentionally checks .days and not .total_seconds()
# since we don't want to switch over to buffered if they go
# over one day by a few hours since the UI makes it so easy to do that.
if entity_ids or context_id or (end_day - start_day).days <= 1:
rows = query.all() rows = query.all()
else: else:
rows = query.yield_per(1000) # Only buffer rows to reduce memory pressure
# if we expect the result set is going to be very large.
# What is considered very large is going to differ
# based on the hardware Home Assistant is running on.
#
# sqlalchemy suggests that is at least 10k, but for
# even and RPi3 that number seems higher in testing
# so we don't switch over until we request > 1 day+ of data.
#
rows = query.yield_per(1024)
for row in rows: for row in rows:
context_lookup.setdefault(row.context_id, row) context_lookup.setdefault(row.context_id, row)
if row.context_only: if row.context_only: