Call sqlite pragma optimize during periodic cleanup task (#91245)

https://www.sqlite.org/pragma.html#pragma_optimize

> To achieve the best long-term query performance without the need to do a detailed engineering analysis of the application schema and SQL, it is recommended that applications run "PRAGMA optimize" (with no arguments) just before closing each database connection. Long-running applications might also benefit from setting a timer to run "PRAGMA optimize" every few hours.

> This pragma is usually a no-op or nearly so and is very fast.

Since we keep the recorder connection open for the entire time HA
is running we fall into the long-running application bucket
This commit is contained in:
J. Nick Koston 2023-04-11 16:39:19 -10:00 committed by GitHub
parent 4e6937d20f
commit e40a373c4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -673,6 +673,7 @@ def periodic_db_cleanups(instance: Recorder) -> None:
_LOGGER.debug("WAL checkpoint")
with instance.engine.connect() as connection:
connection.execute(text("PRAGMA wal_checkpoint(TRUNCATE);"))
connection.execute(text("PRAGMA OPTIMIZE;"))
@contextmanager