[FIX] product: fix performance tree/kanban views product

The compute of `product_variant_count` was highly inefficient
in batch. It is due to `with_prefetch` which remove the prefetch
(reset _prefetch_ids = _ids) of the for loop.
It was added by 05a00fa9d4
which fixed performance issue for the 11.0 about
`sales_count`. But with the new ORM (13.0) compute fields are lazy.

Performance improvements:
For the search_read of the kanban view (Menu: Sale/Products/Products)
with the default pagination (80 items):
Before: 347 SQL request and 0.31 +- 0.2 sec (Python and SQL time)
After: 110 SQL request and 0.14 +- 0.2 sec (Python and SQL time)

task-2439019
This commit is contained in:
Rémy Voet (ryv)
2021-03-30 09:27:05 +00:00
parent 5dee24c670
commit 9253643fd0
+1 -2
View File
@@ -345,8 +345,7 @@ class ProductTemplate(models.Model):
@api.depends('product_variant_ids.product_tmpl_id')
def _compute_product_variant_count(self):
for template in self:
# do not pollute variants to be prefetched when counting variants
template.product_variant_count = len(template.with_prefetch().product_variant_ids)
template.product_variant_count = len(template.product_variant_ids)
@api.depends('product_variant_ids', 'product_variant_ids.default_code')
def _compute_default_code(self):