Index spree_state_changes.stateful_id column #5719
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.
What? Why?
This speeds up a great deal one of the most awful queries our DB servers execute. It's not rare to see traces above 20s in Datadog 😱. Source: https://app.datadoghq.com/apm/traces?from_ts=1591104006364&index=apm-search&live=true&query=env%3Aproduction+service%3Arails-postgres+operation_name%3Apostgres.query+resource_name%3A%22SELECT+COUNT+%28+%2A+%29+FROM+spree_state_changes+WHERE+spree_state_changes+.+stateful_id+%3D+%3F+AND+spree_state_changes+.+stateful_type+%3D+%3F%22&spanID&stream_sort=desc&to_ts=1593696006364&trace&traceID
In staging, with no traffic, we go from
to
There's no point on indexing any other column because apparently there's only a
stateful_type
,Spree::Order
.What should we test?
There's not much we can test. As far as I know this query is only executed when accessing /admin/orders/Rxxxxx/edit. It should keep working but an index can't break it anyway.
Release notes
Speed up order edition from admin and lower the pressure it exerts on the DB.
Changelog Category: Changed