From bdc8c184fded121ffc00ff395b922f1bd5590695 Mon Sep 17 00:00:00 2001 From: JDutil Date: Mon, 22 Jul 2019 16:21:41 -0600 Subject: [PATCH] Use pluck(:value).first to prevent ORDER BY in SQL query 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. --- backend/app/views/spree/admin/promotions/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/views/spree/admin/promotions/index.html.erb b/backend/app/views/spree/admin/promotions/index.html.erb index 5b733b0193d..0b208d06faa 100644 --- a/backend/app/views/spree/admin/promotions/index.html.erb +++ b/backend/app/views/spree/admin/promotions/index.html.erb @@ -78,7 +78,7 @@ <%= promotion.name %> - <%= (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) %>