From f7d4dcf9812cb7044e112b8fe813c6c8a04807b7 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink <378974+boboldehampsink@users.noreply.github.com> Date: Tue, 7 Nov 2023 12:53:55 +0100 Subject: [PATCH 1/2] Use subquery in hasVariant criteria to improve performance --- src/elements/db/ProductQuery.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/elements/db/ProductQuery.php b/src/elements/db/ProductQuery.php index 9bd6d6e935..4332978d03 100644 --- a/src/elements/db/ProductQuery.php +++ b/src/elements/db/ProductQuery.php @@ -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]); } /** From a18d57ba67610d3791d5e3b4b0c5482e0e6131e4 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink <378974+boboldehampsink@users.noreply.github.com> Date: Tue, 7 Nov 2023 13:04:20 +0100 Subject: [PATCH 2/2] Also apply for VariantQuery::hasProduct --- src/elements/db/VariantQuery.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/elements/db/VariantQuery.php b/src/elements/db/VariantQuery.php index 1c4a136b4a..d5cadac2cd 100644 --- a/src/elements/db/VariantQuery.php +++ b/src/elements/db/VariantQuery.php @@ -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]); } /**