Skip to content

Commit d806ebf

Browse files
committed
MCP-946: Price Index Optimization
1 parent 9a92e3e commit d806ebf

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

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

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -357,54 +357,65 @@ 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-
360+
$productLinkFieldsToEntityIdMap = $this->connection->fetchPairs(
361+
$this->connection->select()->from(
362+
['cpe' => $this->getTable('catalog_product_entity')],
363+
[$linkField, 'entity_id']
364+
)->where(
365+
'cpe.entity_id IN (?)',
366+
$productIds,
367+
Zend_Db::INT_TYPE
368+
)
369+
);
361370
foreach ($attributeTypes as $backendType => $attributeIds) {
362371
if ($attributeIds) {
363-
foreach ($attributeIds as $attributeId) {
364-
$tableName = $this->getTable('catalog_product_entity_' . $backendType);
365-
$select = $this->connection->select()->from(
366-
't',
367-
[
368-
'entity_id' => 't.entity_id',
369-
'value' => $this->unifyField($ifStoreValue, $backendType),
370-
]
371-
)->joinLeft(
372-
['t_default' => $tableName],
373-
't.' . $linkField . ' = t_default.' . $linkField
374-
. ' AND t_default.attribute_id = ' . (int) $attributeId
375-
. ' AND t_default.store_id = 0',
376-
['attribute_id' => 't_default.attribute_id']
377-
)->joinLeft(
378-
['t_store' => $tableName],
379-
't.' . $linkField . ' = t_store.' . $linkField
380-
. ' AND t_store.attribute_id = ' . (int) $attributeId
381-
. ' AND t_store.store_id = ' . (int) $storeId,
382-
383-
[]
384-
);
385-
$selects[] = $select;
386-
}
387-
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;
388407
}
389408
}
390409

391410
if ($selects) {
392-
$select = 'WITH t AS (' .
393-
$this->connection->select()->from(
394-
['cpe' => $this->getTable('catalog_product_entity')],
395-
$linkField != 'entity_id' ? [$linkField, 'entity_id'] : ['entity_id']
396-
)->where(
397-
'cpe.entity_id IN (?)',
398-
$productIds,
399-
Zend_Db::INT_TYPE
400-
) . ') '.
401-
$this->connection->select()->union($selects, Select::SQL_UNION_ALL);
411+
$select = $this->connection->select()->union($selects, Select::SQL_UNION_ALL);
402412
$query = $this->connection->query($select);
403413
while ($row = $query->fetch()) {
404-
$entityId = $row['entity_id'];
414+
$entityId = $productLinkFieldsToEntityIdMap[$row[$linkField]];
405415
$result[$entityId][$row['attribute_id']] = $row['value'];
406416
}
407417
}
418+
408419
return $result;
409420
}
410421

0 commit comments

Comments
 (0)