From 1ff665a33a414a77d68145498636ada41c82d8da Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Tue, 24 Mar 2026 02:24:40 +0500 Subject: [PATCH 1/2] Refactor permitted producer IDs and outgoing exchange variant IDs queries for improved performance --- .../order_management/subscriptions/variants_list.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/engines/order_management/app/services/order_management/subscriptions/variants_list.rb b/engines/order_management/app/services/order_management/subscriptions/variants_list.rb index f3a817219a..771a231d7f 100644 --- a/engines/order_management/app/services/order_management/subscriptions/variants_list.rb +++ b/engines/order_management/app/services/order_management/subscriptions/variants_list.rb @@ -30,17 +30,19 @@ module OrderManagement other_permitted_producer_ids = EnterpriseRelationship.joins(:parent) .permitting(distributor.id).with_permission(:add_to_order_cycle) .merge(Enterprise.is_primary_producer) - .pluck(:parent_id) + .select(:parent_id) # Append to the potentially gigantic array instead of using union, which creates a new array # The db IN statement won't care if there's a duplicate. - other_permitted_producer_ids << distributor.id + Enterprise.where(id: distributor.id) + .select(:id) + .or(Enterprise.where(id: other_permitted_producer_ids)) end def self.outgoing_exchange_variant_ids(distributor) - ExchangeVariant.select("DISTINCT exchange_variants.variant_id").joins(:exchange) + ExchangeVariant.joins(:exchange) .where(exchanges: { incoming: false, receiver_id: distributor.id }) - .pluck(:variant_id) + .select(:variant_id) end end end From fc123b38b450b885725a20b0b1ea8c4f9f581fb3 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Tue, 24 Mar 2026 03:46:17 +0500 Subject: [PATCH 2/2] Add comment for outgroing exchange variants --- .../services/order_management/subscriptions/variants_list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engines/order_management/app/services/order_management/subscriptions/variants_list.rb b/engines/order_management/app/services/order_management/subscriptions/variants_list.rb index 771a231d7f..4a119598e1 100644 --- a/engines/order_management/app/services/order_management/subscriptions/variants_list.rb +++ b/engines/order_management/app/services/order_management/subscriptions/variants_list.rb @@ -32,14 +32,14 @@ module OrderManagement .merge(Enterprise.is_primary_producer) .select(:parent_id) - # Append to the potentially gigantic array instead of using union, which creates a new array - # The db IN statement won't care if there's a duplicate. Enterprise.where(id: distributor.id) .select(:id) .or(Enterprise.where(id: other_permitted_producer_ids)) end def self.outgoing_exchange_variant_ids(distributor) + # DISTINCT is not required here since this subquery is used within an IN clause, + # where duplicate values do not impact the result. ExchangeVariant.joins(:exchange) .where(exchanges: { incoming: false, receiver_id: distributor.id }) .select(:variant_id)