Skip to content

Commit

Permalink
[14.0][IMP] Query performance for shop route in website_sale_filter_p…
Browse files Browse the repository at this point in the history
…roduct_brand

Qweb key `attr_valid` stores a list of product ids, which is exposed
to qContext

- Before this commit:

  Product ids list for `attr_valid` is built with ORM `mapped()`,
  which is very slow if you have a lot of products and attributes for
  products.

  (Tested with 15'000 products and around 500 values for different
  attributes)

  Depending on hardware, this might cause some annoying delay before
  the "/shop" route is loaded, as this single query might take several
  seconds to be executed in such situations.

- After this commit:

  Product ids list for `attr_valid` is now built with a search domain
  on `product.attribute.value` model: this will improve loading time
  by a lot (about x35 times faster) for "/shop" route compared to
  ORM `mapped()` execution time.
  • Loading branch information
FrancescoBallerini committed Oct 17, 2023
1 parent 9b328f2 commit 55c6f79
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions website_sale_filter_product_brand/controllers/website_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ def shop(self, page=0, category=None, brand=None, ppg=False, search="", **post):
)
brands = self._remove_extra_brands(brands, search_products, attrib_values)

# use search() domain instead of mapped() for better performance:
# will basically search for product's related attribute values
attrib_valid_ids = (
request.env["product.attribute.value"]
.search(
[
"&",
(
"pav_attribute_line_ids.product_tmpl_id",
"in",
search_products._ids,
),
("pav_attribute_line_ids.value_ids", "!=", False),
]
)
.ids
)

# keep selected brands in URL
keep = QueryURL(
"/shop",
Expand All @@ -105,9 +123,7 @@ def shop(self, page=0, category=None, brand=None, ppg=False, search="", **post):
{
"brands": brands,
"selected_brand_ids": selected_brand_ids,
"attr_valid": search_products.mapped(
"attribute_line_ids.value_ids"
).ids,
"attr_valid": attrib_valid_ids,
"keep": keep,
}
)
Expand Down

0 comments on commit 55c6f79

Please sign in to comment.