diff --git a/CHANGELOG.md b/CHANGELOG.md index f595f2ba15..641f4262cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Improved product query performance. ([#4281](https://github.com/craftcms/commerce/issues/4281)) - Fixed a bug where Users’ orders, carts, and subscriptions weren’t retaining their view options on the dedicated “Commerce” screen within Edit User. ## 5.6.2 - 2026-04-15 diff --git a/src/elements/db/ProductQuery.php b/src/elements/db/ProductQuery.php index 168c9c7767..bce927bc1b 100644 --- a/src/elements/db/ProductQuery.php +++ b/src/elements/db/ProductQuery.php @@ -755,7 +755,10 @@ protected function afterPrepare(): bool $this->subQuery->leftJoin(['catalogprices' => $catalogPricesQuery], '[[catalogprices.purchasableId]] = [[commerce_products.defaultVariantId]] AND [[catalogprices.storeId]] = [[sitestores.storeId]]'); } else { - $this->subQuery->leftJoin(['purchasablesstores' => Table::PURCHASABLES_STORES], '[[purchasablesstores.storeId]] = [[sitestores.storeId]] AND [[purchasablesstores.purchasableId]] = [[commerce_products.defaultVariantId]]'); + // For speed in Postgres we only need this if the `defaultPrice` criteria is being used. + if (isset($this->defaultPrice)) { + $this->subQuery->leftJoin(['purchasablesstores' => Table::PURCHASABLES_STORES], '[[purchasablesstores.storeId]] = [[sitestores.storeId]] AND [[purchasablesstores.purchasableId]] = [[commerce_products.defaultVariantId]]'); + } } return parent::afterPrepare();