From ee4f2a7b02f9dc862c7f574032a1f1a0aa002149 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 1 Feb 2021 14:35:53 +0100 Subject: [PATCH] Remove N+1 on variants and products We get from an initial INNER JOIN with variants and products to fetch the variant overrides + N queries like: ```sql SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."id" = $1 LIMIT 1 [["id", 1545]] SELECT "spree_products".* FROM "spree_products" WHERE "spree_products"."id" = $1 LIMIT 1 [["id", 604]] ``` to the same initial INNER JOIN + just 2 queries like: ```sql SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."id" IN (1551, 1554) SELECT "spree_products".* FROM "spree_products" WHERE "spree_products"."deleted_at" IS NULL AND "spree_products"."id" IN (606, 607) ``` --- app/controllers/admin/variant_overrides_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index 7e4359a7f4..3172201770 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -76,6 +76,7 @@ module Admin def collection @variant_overrides = VariantOverride. joins(variant: :product). + preload(variant: :product). for_hubs(params[:hub_id] || @hubs) end