Skip to content

Commit b2dd08c

Browse files
committed
ACP2E-3389: Adobe Commerce backend loads categories very slowly
1 parent 29e4a5f commit b2dd08c

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Category;
77

@@ -337,36 +337,7 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
337337
$categoryIds = array_keys($anchor);
338338
$countSelect = $this->getProductsCountQuery($categoryIds, (bool)$websiteId);
339339
$categoryProductsCount = $this->_conn->fetchPairs($countSelect);
340-
341-
$subSelect = clone $this->_conn->select();
342-
$subSelect->from(['ce2' => $this->getTable('catalog_category_entity')], 'ce2.entity_id')
343-
->where("ce2.path LIKE CONCAT(ce.path, '/%')");
344-
345-
$select = clone $this->_conn->select();
346-
$select->from(['ce' => $this->getTable(
347-
'catalog_category_entity')],
348-
'ce.entity_id'
349-
);
350-
$joinCondition = new \Zend_Db_Expr("ce.entity_id=cp.category_id OR cp.category_id IN ({$subSelect})");
351-
$select->joinLeft([
352-
'cp' => $this->getProductTable()],
353-
$joinCondition,
354-
'COUNT(DISTINCT cp.product_id) AS product_count'
355-
);
356-
if ($websiteId) {
357-
$select->join(
358-
['w' => $this->getProductWebsiteTable()],
359-
'cp.product_id = w.product_id',
360-
[]
361-
)->where(
362-
'w.website_id = ?',
363-
$websiteId
364-
);
365-
}
366-
$select->where('ce.entity_id IN(?)', $categoryIds);
367-
$select->group('ce.entity_id');
368-
369-
$countFromCategoryTable = $this->_conn->fetchPairs($select);
340+
$countFromCategoryTable = $this->getCountFromCategoryTable($categoryIds, (int)$websiteId);
370341

371342
foreach ($anchor as $item) {
372343
$productsCount = 0;
@@ -381,6 +352,48 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
381352
return $this;
382353
}
383354

355+
/**
356+
* Get products number for each category with bulk query
357+
*
358+
* @param array $categoryIds
359+
* @param int $websiteId
360+
* @return array
361+
*/
362+
private function getCountFromCategoryTable(
363+
array $categoryIds,
364+
int $websiteId
365+
) : array {
366+
$subSelect = clone $this->_conn->select();
367+
$subSelect->from(['ce2' => $this->getTable('catalog_category_entity')], 'ce2.entity_id')
368+
->where("ce2.path LIKE CONCAT(ce.path, '/%')");
369+
370+
$select = clone $this->_conn->select();
371+
$select->from(
372+
['ce' => $this->getTable('catalog_category_entity')],
373+
'ce.entity_id'
374+
);
375+
$joinCondition = new \Zend_Db_Expr("ce.entity_id=cp.category_id OR cp.category_id IN ({$subSelect})");
376+
$select->joinLeft(
377+
['cp' => $this->getProductTable()],
378+
$joinCondition,
379+
'COUNT(DISTINCT cp.product_id) AS product_count'
380+
);
381+
if ($websiteId) {
382+
$select->join(
383+
['w' => $this->getProductWebsiteTable()],
384+
'cp.product_id = w.product_id',
385+
[]
386+
)->where(
387+
'w.website_id = ?',
388+
$websiteId
389+
);
390+
}
391+
$select->where('ce.entity_id IN(?)', $categoryIds);
392+
$select->group('ce.entity_id');
393+
394+
return $this->_conn->fetchPairs($select);
395+
}
396+
384397
/**
385398
* Add category path filter
386399
*

0 commit comments

Comments
 (0)