From c21141e18039f7d026e24b6f401c6620919166eb Mon Sep 17 00:00:00 2001 From: Andrii Voskoboinikov Date: Mon, 14 May 2018 12:20:03 +0300 Subject: [PATCH] MAGETWO-91114: [2.3 Forwardport] Performance of Category Products index is slow when you have big number of categories --- .../Category/Product/AbstractAction.php | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php b/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php index b44a173e1b91d..afc2cd53ce417 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php @@ -659,25 +659,30 @@ protected function makeTempCategoryTreeIndex() */ protected function fillTempCategoryTreeIndex($temporaryName) { - // This finds all children (cc2) that descend from a parent (cc) by path. - // For example, cc.path may be '1/2', and cc2.path may be '1/2/3/4/5'. - $temporarySelect = $this->connection->select()->from( - ['cc' => $this->getTable('catalog_category_entity')], - ['parent_id' => 'entity_id'] - )->joinInner( - ['cc2' => $this->getTable('catalog_category_entity')], - 'cc2.path LIKE ' . $this->connection->getConcatSql( - [$this->connection->quoteIdentifier('cc.path'), $this->connection->quote('/%')] - ), - ['child_id' => 'entity_id'] + $selects = $this->prepareSelectsByRange( + $this->connection->select() + ->from( + ['c' => $this->getTable('catalog_category_entity')], + ['entity_id', 'path'] + ), + 'entity_id' ); - $this->connection->query( - $temporarySelect->insertFromSelect( - $temporaryName, - ['parent_id', 'child_id'] - ) - ); + foreach ($selects as $select) { + $values = []; + + foreach ($this->connection->fetchAll($select) as $category) { + foreach (explode('/', $category['path']) as $parentId) { + if ($parentId !== $category['entity_id']) { + $values[] = [$parentId, $category['entity_id']]; + } + } + } + + if (count($values) > 0) { + $this->connection->insertArray($temporaryName, ['parent_id', 'child_id'], $values); + } + } } /**