mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 05:37:44 +00:00
Remove unneeded lambda_stmt in place add in statistics (#89943)
We can generate this entire query in a single lambda_stmt so there is no need to add two which increases the size of the cache key
This commit is contained in:
parent
f27d73fc34
commit
817ba97227
@ -1969,24 +1969,24 @@ def _latest_short_term_statistics_stmt(
|
|||||||
metadata_ids: list[int],
|
metadata_ids: list[int],
|
||||||
) -> StatementLambdaElement:
|
) -> StatementLambdaElement:
|
||||||
"""Create the statement for finding the latest short term stat rows."""
|
"""Create the statement for finding the latest short term stat rows."""
|
||||||
stmt = lambda_stmt(lambda: select(*QUERY_STATISTICS_SHORT_TERM))
|
return lambda_stmt(
|
||||||
stmt += lambda s: s.join(
|
lambda: select(*QUERY_STATISTICS_SHORT_TERM).join(
|
||||||
(
|
(
|
||||||
most_recent_statistic_row := (
|
most_recent_statistic_row := (
|
||||||
select(
|
select(
|
||||||
StatisticsShortTerm.metadata_id,
|
StatisticsShortTerm.metadata_id,
|
||||||
# https://github.com/sqlalchemy/sqlalchemy/issues/9189
|
# https://github.com/sqlalchemy/sqlalchemy/issues/9189
|
||||||
# pylint: disable-next=not-callable
|
# pylint: disable-next=not-callable
|
||||||
func.max(StatisticsShortTerm.start_ts).label("start_max"),
|
func.max(StatisticsShortTerm.start_ts).label("start_max"),
|
||||||
)
|
)
|
||||||
.where(StatisticsShortTerm.metadata_id.in_(metadata_ids))
|
.where(StatisticsShortTerm.metadata_id.in_(metadata_ids))
|
||||||
.group_by(StatisticsShortTerm.metadata_id)
|
.group_by(StatisticsShortTerm.metadata_id)
|
||||||
).subquery()
|
).subquery()
|
||||||
),
|
),
|
||||||
(StatisticsShortTerm.metadata_id == most_recent_statistic_row.c.metadata_id)
|
(StatisticsShortTerm.metadata_id == most_recent_statistic_row.c.metadata_id)
|
||||||
& (StatisticsShortTerm.start_ts == most_recent_statistic_row.c.start_max),
|
& (StatisticsShortTerm.start_ts == most_recent_statistic_row.c.start_max),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return stmt
|
|
||||||
|
|
||||||
|
|
||||||
def get_latest_short_term_statistics(
|
def get_latest_short_term_statistics(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user