With several category rules active and showing a list of products, the number of queries to show a single page quickly balloons to over 1000. This is because the rule requires the terms attached to a given line item+product each time another category rule is applied.

The attached patch statically caches the terms found on a given line item+product combination during a single request. In my case this removed about 900 database queries on a single, uncached page view showing about 15 products, with a few more in the cart.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fearlsgroove created an issue. See original summary.

fearlsgroove’s picture

Oops left a dpm in there.

mglaman’s picture

Status: Needs review » Needs work

Thank you! This thought crossed my mind a week ago about how much of a performance suck this thing is. Here's my initial patch review.

  1. +++ b/commerce_discount_product_category.rules.inc
    @@ -43,31 +43,40 @@ function commerce_discount_product_category_rules_condition_info() {
    +  static $cached_items = array();
    ...
    +    static $product_reference_fields = FALSE;
    

    Can we utilize drupal_static(__FUNCTION__)?

    I like having the option to blow away a static cache elsewhere just in case.

  2. +++ b/commerce_discount_product_category.rules.inc
    @@ -43,31 +43,40 @@ function commerce_discount_product_category_rules_condition_info() {
    +    dpm($cache_key);
    

    Can you remove this dpm()?

armrus’s picture

Please view another way to caching and fixing performance reduces.

kevster’s picture

I just applied this patch against version 7.1.2 and got the following error showing on every page:

Warning: array_intersect(): Argument #1 is not an array in commerce_discount_product_category_has_specified_terms_build() (line 136 of /var/www/sites/xxxx/html/sites/all/modules/contrib/commerce_discount_product_category/commerce_discount_product_category.rules.inc).

running on:
- drupal 7.69
- commerce 7.11
- php 7.0.33

Will try against the dev version.