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
pull/89941/head^2
J. Nick Koston 2023-03-19 16:05:39 -10:00 committed by GitHub
parent f27d73fc34
commit 817ba97227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 17 deletions

View File

@ -1969,24 +1969,24 @@ def _latest_short_term_statistics_stmt(
metadata_ids: list[int],
) -> StatementLambdaElement:
"""Create the statement for finding the latest short term stat rows."""
stmt = lambda_stmt(lambda: select(*QUERY_STATISTICS_SHORT_TERM))
stmt += lambda s: s.join(
(
most_recent_statistic_row := (
select(
StatisticsShortTerm.metadata_id,
# https://github.com/sqlalchemy/sqlalchemy/issues/9189
# pylint: disable-next=not-callable
func.max(StatisticsShortTerm.start_ts).label("start_max"),
)
.where(StatisticsShortTerm.metadata_id.in_(metadata_ids))
.group_by(StatisticsShortTerm.metadata_id)
).subquery()
),
(StatisticsShortTerm.metadata_id == most_recent_statistic_row.c.metadata_id)
& (StatisticsShortTerm.start_ts == most_recent_statistic_row.c.start_max),
return lambda_stmt(
lambda: select(*QUERY_STATISTICS_SHORT_TERM).join(
(
most_recent_statistic_row := (
select(
StatisticsShortTerm.metadata_id,
# https://github.com/sqlalchemy/sqlalchemy/issues/9189
# pylint: disable-next=not-callable
func.max(StatisticsShortTerm.start_ts).label("start_max"),
)
.where(StatisticsShortTerm.metadata_id.in_(metadata_ids))
.group_by(StatisticsShortTerm.metadata_id)
).subquery()
),
(StatisticsShortTerm.metadata_id == most_recent_statistic_row.c.metadata_id)
& (StatisticsShortTerm.start_ts == most_recent_statistic_row.c.start_max),
)
)
return stmt
def get_latest_short_term_statistics(