Skip to content

Commit d6e1cf2

Browse files
committed
MCP-946: Price Index optimization
1 parent 263c17f commit d6e1cf2

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/DataProvider.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,10 @@ public function getProductAttributes($storeId, array $productIds, array $attribu
357357
$selects = [];
358358
$ifStoreValue = $this->connection->getCheckSql('t_store.value_id > 0', 't_store.value', 't_default.value');
359359
$linkField = $this->metadata->getLinkField();
360-
$tmpTableName = 'index_fulltext_tmp_' . time();
361-
$this->connection->query('CREATE TEMPORARY TABLE ' . $tmpTableName . ' ' .
360+
$productLinkFieldsToEntityIdMap = $this->connection->fetchPairs(
362361
$this->connection->select()->from(
363362
['cpe' => $this->getTable('catalog_product_entity')],
364-
$linkField != 'entity_id' ? [$linkField, 'entity_id'] : ['entity_id']
363+
[$linkField, 'entity_id']
365364
)->where(
366365
'cpe.entity_id IN (?)',
367366
$productIds,
@@ -370,45 +369,53 @@ public function getProductAttributes($storeId, array $productIds, array $attribu
370369
);
371370
foreach ($attributeTypes as $backendType => $attributeIds) {
372371
if ($attributeIds) {
373-
foreach ($attributeIds as $attributeId) {
374-
$tableName = $this->getTable('catalog_product_entity_' . $backendType);
375-
376-
$select = $this->connection->select()->from(
377-
['t' => $tmpTableName],
378-
[
379-
'entity_id' => 't.entity_id',
380-
$linkField => 't.' . $linkField,
381-
'value' => $this->unifyField($ifStoreValue, $backendType),
382-
]
383-
)->joinLeft(
384-
['t_default' => $tableName],
385-
't.' . $linkField . ' = t_default.' . $linkField
386-
. ' AND t_default.attribute_id = ' . (int) $attributeId
387-
. ' AND t_default.store_id = 0',
388-
['attribute_id' => 't_default.attribute_id']
389-
)->joinLeft(
390-
['t_store' => $tableName],
391-
't.' . $linkField . ' = t_store.' . $linkField
392-
. ' AND t_store.attribute_id = ' . (int) $attributeId
393-
. ' AND t_store.store_id = ' . (int) $storeId,
394-
395-
[]
396-
);
397-
$selects[] = $select;
398-
}
399-
372+
$tableName = $this->getTable('catalog_product_entity_' . $backendType);
373+
374+
$select = $this->connection->select()->from(
375+
['t' => $tableName],
376+
[
377+
$linkField => 't.' . $linkField,
378+
'attribute_id' => 't.attribute_id',
379+
'value' => $this->unifyField($ifStoreValue, $backendType),
380+
]
381+
)->joinLeft(
382+
['t_store' => $tableName],
383+
$this->connection->quoteInto(
384+
't.' . $linkField . '=t_store.' . $linkField .
385+
' AND t.attribute_id=t_store.attribute_id' .
386+
' AND t_store.store_id = ?',
387+
$storeId
388+
),
389+
[]
390+
)->joinLeft(
391+
['t_default' => $tableName],
392+
$this->connection->quoteInto(
393+
't.' . $linkField . '=t_default.' . $linkField .
394+
' AND t.attribute_id=t_default.attribute_id' .
395+
' AND t_default.store_id = ?',
396+
0
397+
),
398+
[]
399+
)->where(
400+
't.attribute_id IN (?)',
401+
$attributeIds
402+
)->where(
403+
't.' . $linkField . ' IN (?)',
404+
array_keys($productLinkFieldsToEntityIdMap)
405+
)->distinct();
406+
$selects[] = $select;
400407
}
401408
}
402409

403410
if ($selects) {
404411
$select = $this->connection->select()->union($selects, Select::SQL_UNION_ALL);
405412
$query = $this->connection->query($select);
406413
while ($row = $query->fetch()) {
407-
$entityId = $row['entity_id'];
414+
$entityId = $productLinkFieldsToEntityIdMap[$row[$linkField]];
408415
$result[$entityId][$row['attribute_id']] = $row['value'];
409416
}
410417
}
411-
$this->connection->dropTemporaryTable($tmpTableName);
418+
412419
return $result;
413420
}
414421

0 commit comments

Comments
 (0)