Index service_instances.service_plan_id #3065
Merged
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.
A short explanation of the proposed change:
Index the foreign key
service_plan_id
in theservice_instances
table.An explanation of the use cases your change solves
Will lead to faster reads when looking up service instances created from a particular service plan - e.g. when making checks prior to purging a service plan's instances, when deleting and deactivating plans, and when deleting service offerings and spaces.
The above all run
service_plan.service_instances
(or.service_instances_dataset
), which results in the querySELECT * FROM service_instances WHERE (service_instances.service_plan_id = xxx)"
I ran
EXPLAIN ANALYZE
on that query (using a different service plan id each time in a bash for loop). For a sense of scale, the foundation in question had 1,121 service instances and 255 service plans at the time.All times are in milliseconds. Here are the results without an index:
And with the index:
So on this relatively small foundation, that slightly increases planning time in return for a five-fold reduction in execution time (20% faster overall). On one of our large foundations, with around 200,000 service instances and 21,000 service plans, planning time was comparable to the above (in fact a bit faster at 0.058ms) while execution was 15x slower, at 17.290ms. That's without the index, as it's an environment where I can't really do manual experiments in the DB, but I think it's fair to expect a much faster query overall.
For context, in that latter environment,
service_instances
is a 55mb table. The ratio of scans to writes in that table is about 15:1, which I think also weighs in favour of an index.Links to any other associated PRs
n/a
I have reviewed the contributing guide
I have viewed, signed, and submitted the Contributor License Agreement
I have made this pull request to the
main
branchI have run all the unit tests using
bundle exec rake
I have run CF Acceptance Tests