Improve performance of fetching and storing history and events with the database#84870
Merged
Conversation
Reduces database size by ~30%
Reduces database size by ~30%
Reduces database size by ~30%
Reduces database size by ~30%
Contributor
|
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
bdraco
commented
Dec 30, 2022
Reduces database size by ~30%
bdraco
commented
Dec 30, 2022
bdraco
commented
Dec 31, 2022
Member
Author
|
This is working well in testing. Need to build the migration code and add tests next |
bdraco
commented
Dec 31, 2022
bdraco
commented
Jan 1, 2023
balloob
reviewed
Jan 2, 2023
balloob
approved these changes
Jan 2, 2023
Member
Author
|
I think this is good to go but I want to migrate two or three more test blues that I have lying around to be 💯 Will do that later tonight
|
Member
Author
|
Thanks |
Member
Author
|
All 3 of the additional test migrations went well. |
Member
|
For the breaking change aimed at developers, would be nice to have a small dev blog on that. |
Member
Author
|
mkmer
pushed a commit
to mkmer/HomeAssistant_Core
that referenced
this pull request
Jan 3, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Breaking change
This breaking change only affects custom integrations that do not use the
recorderhistoryAPI and access the database directly.The underlying database schema has changed. The documentation for the
eventsandstateshave been updated and is available at the Data Science Portal.Proposed change
Note: The bulk of the lines here are copies of the old tests to make sure that history queries still work during the short (unless they have a giant db) live migration from schema 30->32. We still have support for migrating from db schema 0 (circa 2016) and are already maintaining a few tests for that.
Currently we store all timestamps as strings in UTC isotime. This makes searches slow since we have to search strings to find time ranges as it make the indices much larger (also the reason why InnoDB is currently required when using MySQL ..because of the very large index keys). Every single row had to store at least one string which was always in UTC time.
This change makes returning data via the newer apis much faster as well since we avoid many conversions in the process as they mostly return timestamps as floats
While not the primary goal, this also reduces database size by ~10-30% (on the higher side if you have lots of energy/frequently updating sensors and on the lower side if there are a lot of statistics) as well as size of backups. The database size reductions will become apparent after the next second sunday of the month as we only repack the database monthly. For a real world estimate, my primary production went from 1155682304 bytes to 836296704 bytes
As with previous migrations, History queries will keep working during migration. Logbook will be unavailable until the migration completes
There is effectively no more conversion overhead when fetching history with the newer apis


There is also a nice reduction in commit overhead since there are no complex conversions happening in the ORM anymore which will reduce idle CPU and disk I/O (from the size reduction)

history.pyby copying the old tests and patching the schemaDev testing TODO:
Production testing TODO:
Migration testing
Performance testing
Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.To help with the load of incoming pull requests: