Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve performance of product sorting by price in API (#13741)
Damian Legawiec1bddd434de25
diff --git a/server/Gemfile.lock b/server/Gemfile.lockindex c0a5e28ce2..4fe5db5b16 100644--- a/server/Gemfile.lock+++ b/server/Gemfile.lock@@ -47,7 +47,7 @@ PATHoj (~> 3.16)pagy (~> 43.0)spree_core (= 5.4.0.beta4)- typelizer (~> 0.9)+ typelizer (~> 0.10.0)spree_core (5.4.0.beta4)active_storage_validations (= 1.3.0)acts-as-taggable-on@@ -798,7 +798,7 @@ GEMturbo-rails (2.0.23)actionpack (>= 7.1.0)railties (>= 7.1.0)- typelizer (0.9.1)+ typelizer (0.10.0)benchmarkrailties (>= 6.0.0)tzinfo (2.0.6)@@ -1161,7 +1161,7 @@ CHECKSUMStracking_number (2.4.0) sha256=2a4c6b39cbdfc8326c234626556da1e269c8dfe33478786bea93461331c247fftsort (0.2.0) sha256=9650a793f6859a43b6641671278f79cfead60ac714148aabe4e3f0060480089fturbo-rails (2.0.23) sha256=ee0d90733aafff056cf51ff11e803d65e43cae258cc55f6492020ec1f9f9315f- typelizer (0.9.1) sha256=50a09824765adf94d38397685897a1db0d01fc4a3b9b7eec8ee507c276828f05+ typelizer (0.10.0) sha256=344082184163c5178aefc68c068ce9a2dcfec77979cc149a509f4f96269026d8tzinfo (2.0.6) sha256=8daf828cc77bcf7d63b0e3bdb6caa47e2272dcfaf4fbfe46f8c3a9df087a829bunaccent (0.4.0) sha256=d9278230016edc5317c053f8e84c1e1dea6827dec028e5481e2294a4e11e7333unicode-display_width (3.2.0) sha256=0cdd96b5681a5949cdbc2c55e7b420facae74c4aaf9a9815eee1087cb1853c42diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rbindex 59571ffb2c..a3105ae813 100644--- a/spree/core/app/models/concerns/spree/product_scopes.rb+++ b/spree/core/app/models/concerns/spree/product_scopes.rb@@ -41,32 +41,28 @@ module Spreeorder(price_table_name => { amount: :desc })end- # Price sorting scopes that use subqueries to get prices across all variants- # These ensure products with only variant prices (no master price) are included in results+ # Price sorting scopes that use a derived table JOIN to get prices across all variants.+ # These ensure products with only variant prices (no master price) are included in results.add_search_scope :ascend_by_price do- price_subquery = Price- .non_zero- .joins(:variant)- .where("#{Variant.table_name}.product_id = #{Product.table_name}.id")- .select('MIN(amount)')+ price_agg_sql = Price.non_zero.joins(:variant)+ .select("#{Variant.table_name}.product_id AS product_id, MIN(#{Price.table_name}.amount) AS agg_price")+ .group("#{Variant.table_name}.product_id")+ .to_sql- price_sort_sql = "COALESCE((#{price_subquery.to_sql}), 999999999)"-- select("#{Product.table_name}.*", "#{price_sort_sql} AS min_price").- order(Arel.sql("#{price_sort_sql} ASC"))+ joins("LEFT JOIN (#{price_agg_sql}) AS price_agg ON price_agg.product_id = #{Product.table_name}.id").+ select("#{Product.table_name}.*", 'COALESCE(price_agg.agg_price, 999999999) AS min_price').+ order(Arel.sql('COALESCE(price_agg.agg_price, 999999999) ASC'))endadd_search_scope :descend_by_price do- price_subquery = Price- .non_zero- .joins(:variant)- .where("#{Variant.table_name}.product_id = #{Product.table_name}.id")- .select('MAX(amount)')-- price_sort_sql = "COALESCE((#{price_subquery.to_sql}), 0)"-- select("#{Product.table_name}.*", "#{price_sort_sql} AS max_price").- order(Arel.sql("#{price_sort_sql} DESC"))+ price_agg_sql = Price.non_zero.joins(:variant)+ .select("#{Variant.table_name}.product_id AS product_id, MAX(#{Price.table_name}.amount) AS agg_price")+ .group("#{Variant.table_name}.product_id")+ .to_sql++ joins("LEFT JOIN (#{price_agg_sql}) AS price_agg ON price_agg.product_id = #{Product.table_name}.id").+ select("#{Product.table_name}.*", 'COALESCE(price_agg.agg_price, 0) AS max_price').+ order(Arel.sql('COALESCE(price_agg.agg_price, 0) DESC'))endadd_search_scope :price_between do |low, high|