-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve recorder history queries #131702
Improve recorder history queries #131702
Conversation
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
|
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments
In a follow-up PR, we should simplify the RecorderRunsManager
to remove functionality which is no longer needed when the history API doesn't use it.
if not result: | ||
ts = None | ||
else: | ||
ts = result[0].last_updated_ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we allow a scalar option to or add a scalar version of execute_stmt_lambda_element
to avoid these extra checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are other places it could be used, it sure would be nice to avoid the boilerplate checks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there are. For example here:
core/homeassistant/components/recorder/statistics.py
Lines 1279 to 1281 in f428958
if stats := cast(Sequence[Row], execute_stmt_lambda_element(session, stmt)): | |
return dt_util.utc_from_timestamp(stats[0].start_ts) | |
return None |
I think we should do this in a separate PR though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate PR for sure 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more comment about the choice of timestamp for the oldest state, I wonder if this may be a dealbreaker for the approach in this PR?
On second thought, it's probably fine to use last_updated
because:
- States are inserted in the states table in
last_updated
order, notlast_changed
order - States are purged according to their
last_updated
timestamps - The history API selects states according to their
last_updated
timestamps, not according to theirlast_changed
timestamps
Set to draft until I've discussed the implementation with @bdraco |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested on production, did manual purge. Everything appears to work as expected
* Improve recorder history queries * Remove some comments * Update StatesManager._oldest_ts when adding pending state * Update after review * Improve tests * Improve post-purge logic * Avoid calling dt_util.utc_to_timestamp in new code --------- Co-authored-by: J. Nick Koston <[email protected]>
Proposed change
Improve recorder history query logic which avoids unnecessary database queries
Background
We want to avoid unnecessary database queries for time periods where we can't possibly have any states.
Without this PR, the recorder runs table is consulted to determine if there may be any states during the requested interval. This doesn't always work well though:
There's more details in this PR #123449
This PR changes the logic to instead consider the timestamp of the oldest state in the database
Type of change
Additional information
Checklist
ruff format 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
.To help with the load of incoming pull requests: