Optimize recorder purge#12448
Merged
balloob merged 1 commit intohome-assistant:devfrom Feb 16, 2018
Merged
Conversation
Contributor
|
Looks reasonable to me. I'll pull this into my 'production' setup which uses a MySQL database. Let's see. |
Contributor
|
Okay - purging is very fast for me now, but it does not seem to delete anything. That's not a bug that your changes introduced - with the current dev it's the same. What the heck? We should definitely fix that. |
balloob
approved these changes
Feb 15, 2018
tinloaf
approved these changes
Feb 15, 2018
Contributor
tinloaf
left a comment
There was a problem hiding this comment.
After lots of whining from my side and lots of hand-holding from the #dev channel, I can confirm that it works. 😆
balloob
reviewed
Feb 18, 2018
| # will delete the protected state when deleting its associated | ||
| # event. Also, we would be producing NULLed foreign keys otherwise. | ||
| protected_events = session.query(States.event_id) \ | ||
| .filter(States.state_id.in_(protected_state_ids)) \ |
Member
There was a problem hiding this comment.
I got a warning from pytest when running the purge tests locally. I think that it is from this line.
tests/components/recorder/test_purge.py::TestRecorderPurge::test_purge_old_events
/Users/paulus/dev/python/py34-home-assistant/lib/python3.4/site-packages/sqlalchemy/sql/default_comparator.py:161: SAWarning: The IN-predicate on "states.state_id" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate. Consider alternative strategies for improved performance.
'strategies for improved performance.' % expr)
/Users/paulus/dev/python/py34-home-assistant/lib/python3.4/site-packages/sqlalchemy/sql/default_comparator.py:161: SAWarning: The IN-predicate on "events.event_id" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate. Consider alternative strategies for improved performance.
'strategies for improved performance.' % expr)
3 tasks
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.
Description:
This is my proposal for simplifying the purge query. It just selects
MAX(id)for protected states. Because recorded states are never modified we know that this row will also haveMAX(last_updated)for thatentity_id.As there are no subqueries, this should be simple to execute for any database.
Marked RFC since I am not 100% certain on the above assumptions. Tested with SQLite.
CC: @tinloaf
Related issue (if applicable): fixes #12374
Checklist:
If the code does not interact with devices:
toxrun successfully.Tests have been added to verify that the new code works.