From c0ab4dd33ef164513a180fbeb60176255f397219 Mon Sep 17 00:00:00 2001 From: Francesco Ballerini Date: Tue, 17 Oct 2023 19:26:28 +0200 Subject: [PATCH] [14.0][IMP] Query performance for shop route in website_sale_filter_product_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 page is loaded. - After this commit: Product ids list for `attr_valid` is now built with a search domain on `product.attribute.value` model: this should improve load speed by a lot (about x35 times faster). --- .../controllers/website_sale.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/website_sale_filter_product_brand/controllers/website_sale.py b/website_sale_filter_product_brand/controllers/website_sale.py index 061d0a8af8..52aaf153c3 100644 --- a/website_sale_filter_product_brand/controllers/website_sale.py +++ b/website_sale_filter_product_brand/controllers/website_sale.py @@ -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", @@ -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, } )