OCA / brand

Manage brands for products and companies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[13.0] product_brand poor performance when having many (250k) products

Rad0van opened this issue · comments

Not sure it can be improved in some way or it's a problem of our instance but: we have more than 250k products in database (many different machines and their spare parts) with one particular brand. When I try to open view listing the brands it takes ages to load. The same looooong waiting happens if I try to click on brand name in Product kanban view. Is the _compute_products_count to blame? Any help would be appreciated.

Probably... Usual optimization techniques are read_group or search_count.

Probably... Usual optimization techniques are read_group or search_count.

Thanx for quick reply. Would you please care to elaborate a bit on how to apply the mentioned? The _compute_products_count only does this:

    @api.depends("product_ids")
    def _compute_products_count(self):
        for brand in self:
            brand.products_count = len(brand.product_ids)

I'd thought this would produce simple SQL query but obviously it is doing something more. I guess from the time it takes it somehow does a query for each Product with that Brand applied. Should I maybe use search_count in this method instead of what is there currently?

It's caching all products data (more or less). You can do something similar (simpler in this case) to:

https://github.com/OCA/contract/blob/4db5c90a1d26c3ac5567f756e35839c4ee3d9492/contract/models/res_partner.py#L24-L39

It's caching all products data (more or less). You can do something similar (simpler in this case) to:

https://github.com/OCA/contract/blob/4db5c90a1d26c3ac5567f756e35839c4ee3d9492/contract/models/res_partner.py#L24-L39

Yes but that's reading the objects and actually using them. AFAIK I do not need to load them just count them. So I guess I should use search_count in _compute_products_count implementation? Will try.

Thanx @pedrobaeza The solution is quite simple. I create a module where I overrode the method to be like this:

class ProductBrand(models.Model):
    _inherit = "product.brand"

    @api.depends("product_ids")
    def _compute_products_count(self):
        contract_model = self.env['product.template']
        for brand in self:
            brand.products_count = contract_model.search_count([('product_brand_id', '=', brand.id)]) 

Is that correct?

Yes, that's it

Was asking just to be sure I do not miss anything. Does it make any sense to create PR and change the main module with this code?

Yes, please go with the pr

OK, will. Thanx for help, closing this one.