Reduce cache key size for queries that only need single columns (#90430)

* Reduce cache key size for queries that only need single columns

These queries only cared about a single row but would select
the whole set of columns from the orm object

* wrap it
This commit is contained in:
J. Nick Koston 2023-03-28 17:28:24 -10:00 committed by GitHub
parent ce28bfe5b2
commit 403dffc12d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -143,7 +143,11 @@ def raise_if_exception_missing_str(ex: Exception, match_substrs: Iterable[str])
def _get_schema_version(session: Session) -> int | None: def _get_schema_version(session: Session) -> int | None:
"""Get the schema version.""" """Get the schema version."""
res = session.query(SchemaChanges).order_by(SchemaChanges.change_id.desc()).first() res = (
session.query(SchemaChanges.schema_version)
.order_by(SchemaChanges.change_id.desc())
.first()
)
return getattr(res, "schema_version", None) return getattr(res, "schema_version", None)

View File

@ -480,6 +480,11 @@ def compile_statistics(instance: Recorder, start: datetime, fire_events: bool) -
return True return True
def _get_first_id_stmt(start: datetime) -> StatementLambdaElement:
"""Return a statement that returns the first run_id at start."""
return lambda_stmt(lambda: select(StatisticsRuns.run_id).filter_by(start=start))
def _compile_statistics( def _compile_statistics(
instance: Recorder, session: Session, start: datetime, fire_events: bool instance: Recorder, session: Session, start: datetime, fire_events: bool
) -> set[str]: ) -> set[str]:
@ -496,7 +501,7 @@ def _compile_statistics(
modified_statistic_ids: set[str] = set() modified_statistic_ids: set[str] = set()
# Return if we already have 5-minute statistics for the requested period # Return if we already have 5-minute statistics for the requested period
if session.query(StatisticsRuns).filter_by(start=start).first(): if execute_stmt_lambda_element(session, _get_first_id_stmt(start)):
_LOGGER.debug("Statistics already compiled for %s-%s", start, end) _LOGGER.debug("Statistics already compiled for %s-%s", start, end)
return modified_statistic_ids return modified_statistic_ids