Check if promotions exist without extra db query #3586
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.
Description
Using
@promotions.any?
causes an extra db query (SELECT DISTINCT ... LIMIT 1
, see below). Instead, we can force-load the dataset and check its length, because it is used few lines down (@promotions.each
).Relevant fragment from logs:
Here, the query in second line is caused by
@promotions.any?
. It disappears after applying this change.Performance:
It can have quite significant (positive) performance impact when there is a lot of promotions and someone is searching (b-tree index does not work for
ILIKE '%f66e%
, so seq scan is used). Sometimes the page even times out on that.According to my tests on local env with ~20 million of promotion codes in 2500 promotions it can save about 20-25% of time used on rendering this view.
Checklist: