From ef90c22efda8a1d56d41eb0739d94733fb574f41 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 1 Sep 2021 07:56:07 +0000 Subject: [PATCH] [FIX] product: speed up _name_search If `_name_search` is called with `limit=None` it could take time in the loop because `_name_search` for `product.product` has `limit=100`, i.e. it could be 100 iterations to scan 10 K products. To solve this problem, call `_name_search` with `limit=None` to get all product variants at once. As an example, the problem is reproduced on using menu `Manufacturing > Products > Bills of Materials`, which makes following search query: ``` ["|", ["code", "ilike", "XXX"], ["product_tmpl_id", "ilike", "XXX"]] ``` for which ORM calls `_name_search`: https://github.com/odoo/odoo/blame/9b23864027bc24032993833f213395cb45fe86bb/odoo/osv/expression.py#L863 In a customer database, this speeds up the query from 70 seconds to 3,5 seconds. --- opw-2631012 closes odoo/odoo#77601 X-original-commit: 038816725a988a0ada3d6f8518a436ea46a81eba Signed-off-by: Ivan Yelizariev // IEL --- addons/product/models/product_template.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 7902b351c01..88cfb214baf 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -540,7 +540,10 @@ class ProductTemplate(models.Model): while True: domain = templates and [('product_tmpl_id', 'not in', templates.ids)] or [] args = args if args is not None else [] - products_ids = Product._name_search(name, args+domain, operator=operator, name_get_uid=name_get_uid) + # Product._name_search has default value limit=100 + # So, we either use that value or override it to None to fetch all products at once + kwargs = {} if limit else {'limit': None} + products_ids = Product._name_search(name, args+domain, operator=operator, name_get_uid=name_get_uid, **kwargs) products = Product.browse(products_ids) new_templates = products.mapped('product_tmpl_id') if new_templates & templates: