[18.0][IMP] website_sale_hide_empty_category: Use _read_group and cache to performance#1226
Conversation
| child.has_product_recursive for child in category.child_id | ||
| website = self.env["website"].get_current_website() | ||
| website_domain = website.sale_product_domain() | ||
| data = self.env["product.template"]._read_group( |
There was a problem hiding this comment.
I'm not sure the read_group is the right choice here, because we don't really care about the grouped products.. so that's just overhead.
How about this instead?
categories = self.search([("product_tmpl_ids", "any", website_domain)])| used_category_ids = set() | ||
| for group in data: | ||
| category = group[0] | ||
| if not category or (category.website_id and category.website_id != website): |
There was a problem hiding this comment.
I would add these filtering conditions to the search domain directly.
If you followed my other comment recommendation, it would be like:
categories = self.search(
[
("website_id", "in", [False, website.id]),
("product_tmpl_ids", "any", website_domain),
]
)| category = group[0] | ||
| if not category or (category.website_id and category.website_id != website): | ||
| continue | ||
| used_category_ids.update( |
There was a problem hiding this comment.
I'm not sure if this works (nested "any" in domain) but possibly this can be part of the search domain too, like this:
categories = self.search(
[
("website_id", "in", [False, website.id]),
"|",
("product_tmpl_ids", "any", website_domain),
("child_ids", "any", ("product_tmpl_ids", "any", website_domain)),
]
)| # TODO: Filter categories before split in c to avoid cache because compute is | ||
| # called with only one category | ||
| categories = self.search(website.website_domain()) | self | ||
| self.env.cache.update_raw( |
There was a problem hiding this comment.
I'm not sure about this respects the ORM, tbh.
For instance if you have other computed fields depending on them, I don't think their recomputations will be triggered like this.
Maybe the cache can be used but not like this.
Perhaps we could check if we already have a cached has_product_recursive value for any of the children categories - -and if we do we set to True directly bypassing the SQL-based computation.
But I'd say first check the performance with the recommendations I provided above, maybe it's not even needed
@Tecnativa