Skip to content

[18.0][IMP] website_sale_hide_empty_category: Use _read_group and cache to performance#1226

Open
sergio-teruel wants to merge 1 commit into
OCA:18.0from
Tecnativa:18.0-IMP-website_sale_hide_empty_category-performance
Open

[18.0][IMP] website_sale_hide_empty_category: Use _read_group and cache to performance#1226
sergio-teruel wants to merge 1 commit into
OCA:18.0from
Tecnativa:18.0-IMP-website_sale_hide_empty_category-performance

Conversation

@sergio-teruel

@sergio-teruel sergio-teruel commented May 27, 2026

Copy link
Copy Markdown
Contributor

@OCA-git-bot OCA-git-bot added series:18.0 mod:website_sale_hide_empty_category Module website_sale_hide_empty_category labels May 27, 2026
@sergio-teruel sergio-teruel changed the title [18.][IMP] website_sale_hide_empty_category: Use _read_group and cache to … [18.0][IMP] website_sale_hide_empty_category: Use _read_group and cache to performance May 27, 2026
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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:website_sale_hide_empty_category Module website_sale_hide_empty_category series:18.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants