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

amelchio
Copy link
Contributor

@amelchio amelchio 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
Copy link
Contributor

corneyl 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
Copy link
Contributor Author

amelchio commented Mar 2, 2018

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

@corneyl
Copy link
Contributor

corneyl 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 mentioned this pull request Mar 3, 2018
3 tasks
@pvizeli pvizeli merged commit 339a839 into home-assistant:dev Mar 3, 2018
@balloob balloob mentioned this pull request Mar 9, 2018
@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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

postgres stuck running cleanup query.
4 participants