From 1e34f41db095f506f07794f0de40bd3ed71ea136 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 10 Apr 2023 12:00:19 -1000 Subject: [PATCH] restore lost performance --- homeassistant/components/recorder/history/modern.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/recorder/history/modern.py b/homeassistant/components/recorder/history/modern.py index cb3c08aeccb..949e00126df 100644 --- a/homeassistant/components/recorder/history/modern.py +++ b/homeassistant/components/recorder/history/modern.py @@ -513,10 +513,9 @@ def _get_states_for_entities_stmt( no_attributes: bool, ) -> Select: """Baked query to get states for specific entities.""" - stmt = _stmt_and_join_attributes(no_attributes, True) # We got an include-list of entities, accelerate the query by filtering already # in the inner query. - stmt = stmt.join( + stmt = _stmt_and_join_attributes(no_attributes, True).join( ( most_recent_states_for_entities_by_date := ( select( @@ -541,11 +540,11 @@ def _get_states_for_entities_stmt( == most_recent_states_for_entities_by_date.c.max_last_updated, ), ) - if not no_attributes: - stmt = stmt.outerjoin( - StateAttributes, (States.attributes_id == StateAttributes.attributes_id) - ) - return stmt + if no_attributes: + return stmt + return stmt.outerjoin( + StateAttributes, (States.attributes_id == StateAttributes.attributes_id) + ) def _get_run_start_ts_from_run_for_utc_point_in_time(