From 8a6b6dee7de9bf7985d6fbeac04de76f50c3d135 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 14 Apr 2021 13:43:29 +0000 Subject: [PATCH] [FIX] mrp: optimize _compute_used_in_bom_count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit previous implementation works slowly for products with many variants. Performance Test ================ * ~5 K product.template * ~14 K product.variants * ~12 K mrp.bom * ~414 K mrp.bom.line Reading time for field ``used_in_bom_count`` on a product with ~1000 variants * Before 9,5 sec * After 1,5 sec --- opw-2494423 closes odoo/odoo#69341 X-original-commit: a2d63e8ae5d6bc0e822d12d724b5e3ff5a979cad Signed-off-by: Rémy Voet Signed-off-by: Ivan Yelizariev // IEL --- addons/mrp/models/product.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/mrp/models/product.py b/addons/mrp/models/product.py index 06ba54b251f..81b4a3a6bbd 100644 --- a/addons/mrp/models/product.py +++ b/addons/mrp/models/product.py @@ -28,7 +28,7 @@ class ProductTemplate(models.Model): def _compute_used_in_bom_count(self): for template in self: template.used_in_bom_count = self.env['mrp.bom'].search_count( - [('bom_line_ids.product_id', 'in', template.product_variant_ids.ids)]) + [('bom_line_ids.product_tmpl_id', '=', template.id)]) def write(self, values): if 'active' in values: @@ -40,7 +40,7 @@ class ProductTemplate(models.Model): def action_used_in_bom(self): self.ensure_one() action = self.env["ir.actions.actions"]._for_xml_id("mrp.mrp_bom_form_action") - action['domain'] = [('bom_line_ids.product_id', 'in', self.product_variant_ids.ids)] + action['domain'] = [('bom_line_ids.product_tmpl_id', '=', self.id)] return action def _compute_mrp_product_qty(self):