fix: reduce number of queries in layered navigation#387
Open
ahuininga-orisha wants to merge 2 commits into
Open
fix: reduce number of queries in layered navigation#387ahuininga-orisha wants to merge 2 commits into
ahuininga-orisha wants to merge 2 commits into
Conversation
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.
Summary
When using the category link renderer in layered navigation, Tweakwise was issuing individual database queries for every category filter item — one query to load the category entity and another to resolve its URL rewrite. On a category page with many subcategory filter options this resulted in an N+1 query problem, slowing down page render time noticeably.
This fix batch-loads all category entities and their URL rewrites in two queries before the filter items are rendered, serving subsequent per-item lookups from an in-memory cache instead.
LinkRenderer: overridesgetItems()to callStrategyHelper::warmUp()before returning items. This triggers a single batch load of all categories (including nested children) referenced by the filter, keyed by store ID to support multi-store setups.StrategyHelper::warmUp(): added public method that collects all category IDs from the item tree (recursively), skips already-cached IDs, loads the remainder viaCategoryCollectionFactoryin one query, then bulk-loads their URL rewrites viaUrlFinderInterface::findAllByData()in a second query. Loaded categories are stored in$categoryCache[categoryId][storeId].StrategyHelper::getCategoryFromItem(): now checks$categoryCachebefore falling back to the single-rowCategoryRepositoryInterface::get()call, eliminating per-item DB hits when warm-up ran.StrategyHelper::preloadUrlRewrites(): setsrequest_pathon each cached category object soCategory::getUrl()skips its own URL rewrite lookup. Previously this method incorrectly passed all cached IDs instead of only the IDs just loaded — fixed to pass$loadedIdsonly.phpcs:ignore DeclareStrictTypesMissingsuppression to properdeclare(strict_types=1);headers.How to test
Scenario 1 — Category filter links render correctly
setup:di:compileandcache:clean.Scenario 2 — No query regressions
MAGE_MODE=developer+ a query counter profiler or Blackfire).url_rewriteandcatalog_category_entityqueries does not scale with the number of filter items — expect exactly 2 batch queries regardless of item count.Scenario 3 — Multi-store