Variant subquery forcing table scan via USE INDEX in multi-site resave#4319
Draft
lukeholder wants to merge 2 commits into
Draft
Variant subquery forcing table scan via USE INDEX in multi-site resave#4319lukeholder wants to merge 2 commits into
lukeholder wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adjusts VariantQuery’s MySQL USE INDEX (sortOrder…) join hint so it’s only applied for unfiltered “listing” queries, avoiding full table scans when resaving/querying specific variants/products in multi-site setups (issue #4305).
Changes:
- Skip the
elements_ownerssortOrderindex hint whenid,ownerId, orprimaryOwnerIdfilters are present. - Keep the hint for unfiltered queries (and no custom
orderBy) to preserve existing listing performance benefits. - Document the fix in the unreleased changelog.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/elements/db/VariantQuery.php | Gates the MySQL USE INDEX (sortOrder…) hint behind a “no specific ID/owner filter” check to prevent forced table scans. |
| CHANGELOG.md | Adds an unreleased entry describing the multi-site resave performance fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+523
to
+526
| // Forcing the use of the `sortOrder` index only when listing (no specific element/owner filter), | ||
| // so MySQL doesn't prefer it over targeted indexes when querying by ID. | ||
| $hasSpecificFilter = !empty($this->id) || !empty($this->ownerId) || !empty($this->primaryOwnerId); | ||
| if (Craft::$app->getDb()->getIsMysql() && $sortOrderIndex !== null && empty($this->orderBy) && !$hasSpecificFilter) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for #4305.
USE INDEX (idx_...)hint onelements_ownersinVariantQuery::beforePrepare()was being applied unconditionally (for all MySQL queries with no customorderBy), including when resaving a specific product/variant by ID.elementId), but the forcedsortOrderindex causes it to scan the fullelements_ownerstable instead — reported at 1.58M+ rows / ~5s per variant in a multi-site setup.id,ownerId, orprimaryOwnerIdis set, leaving MySQL free to choose the right index for filtered queries while still benefiting from the hint during unfiltered listing queries.Test plan
./craft resave/products --elementId X) in a multi-site, multi-store setup and confirm query time drops significantlysortOrderhint)