Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product method missing of this gem causes n+1 query unintentionally #190

Open
channainfo opened this issue Feb 19, 2021 · 0 comments
Open

Comments

@channainfo
Copy link

I ty to fetch 4 (top) products per selected taxon to show on my website feed. You can see the gist here:

https://gist.github.com/channainfo/175196541df06db41da3d50eef13379e

On line 32 I get the taxon_id from the active relation which result in the Spree::Product
https://gist.github.com/channainfo/175196541df06db41da3d50eef13379e#file-feed_taxon_product-rb-L32

causes spree_related_products to fire method_missing eventually trigger undesired queries as below:

  Spree::RelationType Load (0.4ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.3ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.2ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.2ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.2ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.4ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.2ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.1ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.1ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.2ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.1ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.1ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.1ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.1ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.2ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]
  Spree::RelationType Load (0.3ms)  SELECT "spree_relation_types".* FROM "spree_relation_types" WHERE "spree_relation_types"."applies_to" = $1 ORDER BY "spree_relation_types"."name" ASC  [["applies_to", "Spree::Product"]]

To work around to avoid triggering spree_related products method missing, online 32 I had to do this

# taxon_product.taxon_id == taxon.id
taxon_product.read_attribute_before_type_cast('taxon_id') == taxon.id

There should be a better way to handle this to avoid unwanted performance impact and debuggability
https://github.com/spree-contrib/spree_related_products/blob/master/app/models/spree/product_decorator.rb#L51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant