Skip to content

Commit

Permalink
Use pluck(:value).first to prevent ORDER BY in SQL query
Browse files Browse the repository at this point in the history
The SQL queries to determine each promotions code or batches of codes
count are currently the slowest queries on the promotions index page.

In our application containing approximately 32 Million codes and
counting they've significantly slowed down the page by approximately
80-100 seconds currently.

This was caused by the SQL's ORDER BY clause trying to sort all the
records as they were being counted which was not necessary. Then when
calling `.first` to initialize the ActiveRecordR object it would add
even further delay to the rendering approximately 5 seconds.

By using `pluck(:code)` the ORDER BY statement is dropped, and we don't
have to initialize a full ActiveRecord object either.
  • Loading branch information
JDutil committed Jul 22, 2019
1 parent 4cf111f commit bdc8c18
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/promotions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<tr id="<%= spree_dom_id promotion %>">
<td><%= promotion.name %></td>
<td>
<%= (promotion.codes.size == 1) ? promotion.codes.take.try!(:value) : t('spree.number_of_codes', count: promotion.codes.size) %>
<%= (promotion.codes.size == 1) ? promotion.codes.pluck(:value).first : t('spree.number_of_codes', count: promotion.codes.size) %>
</td>
<td>
<span class="pill pill-<%= promotion.active? ? 'active' : 'inactive' %>">
Expand Down

0 comments on commit bdc8c18

Please sign in to comment.