Skip to content
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

Add SQL index to states.event_id #12825

Merged
merged 1 commit into from Mar 3, 2018

Conversation

Projects
None yet
4 participants
@amelchio
Copy link
Contributor

commented Mar 2, 2018

Description:

The States.event_id field references Event.event_id but until now there was no index on the former. This potentially makes Event purging slow (as reported in #12797) because each removed event does a States table scan to check that it is not referenced.

MySQL users will have this index already, automatically added due to the foreign key. However, because we did not actually write States.event_id values until #12580, this index can be useless (ignored due to low cardinality statistics). I tested that the migration code properly removes this automatic index.

This index should also make it possible to return and even improve the optimization in #12608.

Related issue (if applicable): fixes #12797

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.

@amelchio amelchio changed the title Add SQL index to states.event_id [WIP] Add SQL index to states.event_id Mar 2, 2018

@corneyl

This comment has been minimized.

Copy link
Contributor

commented Mar 2, 2018

How can I temporarily add this index using a query? Purging now takes endlessly to complete.

Edit: I think this did it for me (PostgreSQL): CREATE INDEX ix_states_event_id ON states (event_id);

@amelchio

This comment has been minimized.

Copy link
Contributor Author

commented Mar 2, 2018

@corneyl That is correct. Did it improve purge performance for you?

@corneyl

This comment has been minimized.

Copy link
Contributor

commented Mar 2, 2018

Without index the query did not finish within 9 hours, with index it took I think less than a minute to purge around 200k records. So yes, definitely!

@amelchio amelchio changed the title [WIP] Add SQL index to states.event_id Add SQL index to states.event_id Mar 2, 2018

@amelchio amelchio referenced this pull request Mar 3, 2018

Merged

Optimize logbook SQL query again #12881

2 of 3 tasks complete
@pvizeli

pvizeli approved these changes Mar 3, 2018

@pvizeli pvizeli merged commit 339a839 into home-assistant:dev Mar 3, 2018

5 checks passed

WIP ready for review
Details
cla-bot Everyone involved has signed the CLA
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage remained the same at 94.102%
Details
hound No violations found. Woof!

@balloob balloob referenced this pull request Mar 9, 2018

Merged

0.65 #12995

@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.