Skip to content

Commit 7769546

Browse files
authored
Merge pull request #2542 from magento-performance/MAGETWO-91114
[Performance] [2.3 Forwardport] Performance of Category Products index is slow when you have big number of categories
2 parents 14f7b06 + c7e118c commit 7769546

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -659,25 +659,30 @@ protected function makeTempCategoryTreeIndex()
659659
*/
660660
protected function fillTempCategoryTreeIndex($temporaryName)
661661
{
662-
// This finds all children (cc2) that descend from a parent (cc) by path.
663-
// For example, cc.path may be '1/2', and cc2.path may be '1/2/3/4/5'.
664-
$temporarySelect = $this->connection->select()->from(
665-
['cc' => $this->getTable('catalog_category_entity')],
666-
['parent_id' => 'entity_id']
667-
)->joinInner(
668-
['cc2' => $this->getTable('catalog_category_entity')],
669-
'cc2.path LIKE ' . $this->connection->getConcatSql(
670-
[$this->connection->quoteIdentifier('cc.path'), $this->connection->quote('/%')]
671-
),
672-
['child_id' => 'entity_id']
662+
$selects = $this->prepareSelectsByRange(
663+
$this->connection->select()
664+
->from(
665+
['c' => $this->getTable('catalog_category_entity')],
666+
['entity_id', 'path']
667+
),
668+
'entity_id'
673669
);
674670

675-
$this->connection->query(
676-
$temporarySelect->insertFromSelect(
677-
$temporaryName,
678-
['parent_id', 'child_id']
679-
)
680-
);
671+
foreach ($selects as $select) {
672+
$values = [];
673+
674+
foreach ($this->connection->fetchAll($select) as $category) {
675+
foreach (explode('/', $category['path']) as $parentId) {
676+
if ($parentId !== $category['entity_id']) {
677+
$values[] = [$parentId, $category['entity_id']];
678+
}
679+
}
680+
}
681+
682+
if (count($values) > 0) {
683+
$this->connection->insertArray($temporaryName, ['parent_id', 'child_id'], $values);
684+
}
685+
}
681686
}
682687

683688
/**

0 commit comments

Comments
 (0)