Skip to content

Commit

Permalink
Merge pull request #3325 from boboldehampsink/patch-6
Browse files Browse the repository at this point in the history
Use subquery in hasVariant & hasProduct criteria to improve performance
  • Loading branch information
lukeholder authored Nov 10, 2023
2 parents fbd68ad + a18d57b commit 877a045
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/elements/db/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,11 @@ private function _applyHasVariantParam(): void

$variantQuery->limit = null;
$variantQuery->select('commerce_variants.productId');
$productIds = $variantQuery->asArray()->column();

// Remove any blank product IDs (if any)
$productIds = array_filter($productIds);
$variantQuery->andWhere(['not', ['commerce_variants.productId' => null]]);

$this->subQuery->andWhere(['commerce_products.id' => array_values($productIds)]);
$this->subQuery->andWhere(['commerce_products.id' => $variantQuery]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/elements/db/VariantQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,11 @@ private function _applyHasProductParam(): void

$productQuery->limit = null;
$productQuery->select('commerce_products.id');
$productIds = $productQuery->column();

// Remove any blank product IDs (if any)
$productIds = array_filter($productIds);
$this->subQuery->andWhere(['commerce_variants.productId' => $productIds]);
$productQuery->andWhere(['not', ['commerce_products.id' => null]]);

$this->subQuery->andWhere(['commerce_variants.productId' => $productQuery]);
}

/**
Expand Down

0 comments on commit 877a045

Please sign in to comment.