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

Index spree_state_changes.stateful_id column #5719

Merged
merged 1 commit into from Jul 3, 2020

Conversation

sauloperez
Copy link
Contributor

@sauloperez sauloperez commented Jul 2, 2020

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

EXPLAIN ANALYZE SELECT COUNT ( * )
FROM spree_state_changes
WHERE spree_state_changes . stateful_id = 2024
  AND spree_state_changes . stateful_type = 'Spree::Order';

Planning time: 0.142 ms
Execution time: 9.073 ms

to

EXPLAIN ANALYZE SELECT COUNT ( * )
FROM spree_state_changes
WHERE spree_state_changes . stateful_id = 2024
  AND spree_state_changes . stateful_type = 'Spree::Order';

Planning time: 0.284 ms
Execution time: 0.202 ms

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

@sauloperez sauloperez self-assigned this Jul 2, 2020
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 😱.

In staging, with no traffic, we go from

```
EXPLAIN ANALYZE SELECT COUNT ( * )
FROM spree_state_changes
WHERE spree_state_changes . stateful_id = 2024
  AND spree_state_changes . stateful_type = 'Spree::Order';

Planning time: 0.142 ms
Execution time: 9.073 ms
```

to

```
EXPLAIN ANALYZE SELECT COUNT ( * )
FROM spree_state_changes
WHERE spree_state_changes . stateful_id = 2024
  AND spree_state_changes . stateful_type = 'Spree::Order';

Planning time: 0.284 ms
Execution time: 0.202 ms
```
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley left a comment

Choose a reason for hiding this comment

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

Nice find! 🕵️

@sauloperez
Copy link
Contributor Author

Nice find! detective

It feels like this when you find one of those

@filipefurtad0 filipefurtad0 self-assigned this Jul 3, 2020
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Jul 3, 2020
@filipefurtad0
Copy link
Contributor

Hi @sauloperez ,

Checked several orders as admin/superadmin on /admin/orders/Rxxxxx/edit , found nothing unusual regarding this PR.

Moving to ready to go.

@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Jul 3, 2020
@luisramos0 luisramos0 merged commit d2adcd2 into openfoodfoundation:master Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants