-
Notifications
You must be signed in to change notification settings - Fork 704
Fix N+1 query problem in product detail page #6547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.3
Are you sure you want to change the base?
Fix N+1 query problem in product detail page #6547
Conversation
## Problem - findWithSortedClassCategories() for product detail page was not eagerly loading ProductStock and TaxRule - Product::_calc() triggered lazy loading for each ProductClass, causing N+1 queries - For products with 352 ProductClasses, this resulted in 987 queries (2300ms) ## Solution - Added eager loading of ProductStock and TaxRule via leftJoin - Enabled query result cache with eccube_result_cache_lifetime_short - Applied the same optimization pattern already used in findProductsWithSortedClassCategories() for product list page ## Expected Impact (based on helmet.jp optimization memo) - Query reduction: 987 queries → 3 queries (99.7% reduction) - Initial page load: ~60-70% improvement - Second access: Further improvement with cache hit ## Changes 1. ProductRepository::findWithSortedClassCategories() - Added ProductStock and TaxRule to eager loading - Added result cache configuration 2. ProductRepositoryTest::testFindWithSortedClassCategoriesWithManyProductClasses() - Added test with 100 ProductClasses to verify N+1 problem is solved - Uses Doctrine query logger to count queries before/after _calc() - Asserts no additional queries are executed (N+1 solved) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
@nobuhiko テストが落ちているため、修正のほどお願いいたします。 |
## Problem The test `testFindWithSortedClassCategoriesWithManyProductClasses` was failing on MySQL and SQLite3 (but passing on PostgreSQL) because: - Product::_calc() calls getClassName()->getName() at lines 103 and 106 - ClassName was not eagerly loaded, causing lazy loading queries - This resulted in 2-3 additional queries being detected by the test ## Solution Added ClassName eager loading to both methods: - findWithSortedClassCategories() - for product detail page - findProductsWithSortedClassCategories() - for product list page Added leftJoin for cc1.ClassName and cc2.ClassName to prevent lazy loading when Product::_calc() accesses ClassName. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Rector requires strict type comparison in tests. Changed assertEquals to assertSame for string and integer comparisons. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 4.3 #6547 +/- ##
=======================================
Coverage 82.70% 82.71%
=======================================
Files 480 480
Lines 26507 26517 +10
=======================================
+ Hits 21923 21933 +10
Misses 4584 4584
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. Review Summary:
🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
概要
商品詳細ページで発生していたN+1クエリ問題を解決しました。
問題
findWithSortedClassCategories()がProductStockとTaxRuleをEager LoadingしていなかったProduct::_calc()が各ProductClassに対してlazy loadをトリガーし、N+1クエリが発生解決策
eccube_result_cache_lifetime_shortによる結果キャッシュを有効化期待される効果
変更内容
1. ProductRepository::findWithSortedClassCategories()
2. ProductRepositoryTest::testFindWithSortedClassCategoriesWithManyProductClasses()
_calc()実行前後のクエリ数をカウントセキュリティレビュー結果
✅ セキュリティチェック: 合格
詳細なセキュリティレポートはコメントを参照してください。
テスト
参考
EC-CUBE 4.3既存実装(findProductsWithSortedClassCategories)のパターンを商品詳細にも適用
🤖 Generated with Claude Code