From f0b35c00a50542642de4b12956557adc3f39ceff Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Thu, 19 Feb 2026 16:06:49 +0800 Subject: [PATCH 1/2] Fixed product index unnecessarily eager loading variants Only eager load variants for the `variants` and `stock` table attributes, which actually need them. Other variant-related attributes (defaultPrice, defaultSku, etc.) are already fetched via SQL JOINs in ProductQuery::beforePrepare(). Fixes #4236 --- src/elements/Product.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/elements/Product.php b/src/elements/Product.php index 72693c54fa..8cbd45e220 100644 --- a/src/elements/Product.php +++ b/src/elements/Product.php @@ -688,18 +688,10 @@ public static function gqlScopesByContext(mixed $context): array */ public static function prepElementQueryForTableAttribute(ElementQueryInterface $elementQuery, string $attribute): void { - $variantAttributes = [ - 'variants', - 'defaultPrice', - 'defaultPromotionalPrice', - 'defaultSku', - 'defaultWeight', - 'defaultLength', - 'defaultWidth', - 'defaultHeight', - ]; - - if (in_array($attribute, $variantAttributes, false)) { + // Only eager load variants for attributes that actually need them. + // Other variant-related attributes (defaultPrice, defaultSku, etc.) are already + // fetched via SQL JOINs in ProductQuery::beforePrepare() + if (in_array($attribute, ['variants', 'stock'], true)) { $elementQuery->andWith('variants'); } else { parent::prepElementQueryForTableAttribute($elementQuery, $attribute); From 342bf8b1f5a6c28f5ce9814dbfdfa31dd876bc20 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Thu, 19 Feb 2026 16:10:11 +0800 Subject: [PATCH 2/2] Add changelog entry for #4236 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2612670789..bd2ca49f63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Craft Commerce +## Unreleased + +- Improved product index performance by not eager-loading variants for table attributes that are already fetched via SQL joins. ([#4236](https://github.com/craftcms/commerce/issues/4236)) + ## 5.5.4 - 2026-02-18 - Fixed a bug where subscription plan edit screens weren’t showing their linked description entries, if the entries were disabled. ([#4229](https://github.com/craftcms/commerce/issues/4229))