Skip to content

Optimize recorder purge#12448

Merged
balloob merged 1 commit intohome-assistant:devfrom
amelchio:purge-maxid
Feb 16, 2018
Merged

Optimize recorder purge#12448
balloob merged 1 commit intohome-assistant:devfrom
amelchio:purge-maxid

Conversation

@amelchio
Copy link
Copy Markdown
Contributor

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 have MAX(last_updated) for that entity_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:

  • The code change is tested and works locally.

If the code does not interact with devices:

  • Local tests with tox run successfully.
  • Tests have been added to verify that the new code works.

@tinloaf
Copy link
Copy Markdown
Contributor

tinloaf commented Feb 15, 2018

Looks reasonable to me. I'll pull this into my 'production' setup which uses a MySQL database. Let's see.

@tinloaf
Copy link
Copy Markdown
Contributor

tinloaf commented Feb 15, 2018

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.

Copy link
Copy Markdown
Contributor

@tinloaf tinloaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After lots of whining from my side and lots of hand-holding from the #dev channel, I can confirm that it works. 😆

@amelchio amelchio changed the title [RFC] Optimize recorder purge Optimize recorder purge Feb 15, 2018
@amelchio amelchio added this to the 0.63.3 milestone Feb 15, 2018
@balloob balloob merged commit 0e2d98d into home-assistant:dev Feb 16, 2018
balloob pushed a commit that referenced this pull request Feb 17, 2018
@balloob balloob mentioned this pull request Feb 17, 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)) \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@home-assistant home-assistant locked and limited conversation to collaborators May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MySQL High CPU usage/long running query against states table

4 participants