From d215cdc563d21b74b26c38ec9f95d63341f09347 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 13 May 2022 12:10:43 -0400 Subject: [PATCH] Avoid buffering logbook rows unless we are selecting less than a days worth (#71809) --- homeassistant/components/logbook/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index d850df847a0..592831badf1 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -445,10 +445,22 @@ def _get_events( def yield_rows(query: Query) -> Generator[Row, None, None]: """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() 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: context_lookup.setdefault(row.context_id, row) if row.context_only: