|
6 | 6 | namespace Magento\Catalog\Model\Indexer\Product\Flat;
|
7 | 7 |
|
8 | 8 | use Magento\Catalog\Model\Indexer\Product\Flat\Table\BuilderInterfaceFactory;
|
| 9 | +use Magento\Store\Model\Store; |
9 | 10 |
|
| 11 | +/** |
| 12 | + * Flat product indexer temporary table builder. |
| 13 | + */ |
10 | 14 | class TableBuilder
|
11 | 15 | {
|
12 | 16 | /**
|
@@ -257,75 +261,70 @@ protected function _fillTemporaryTable(
|
257 | 261 | $valueFieldSuffix,
|
258 | 262 | $storeId
|
259 | 263 | ) {
|
260 |
| - $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); |
261 | 264 | if (!empty($tableColumns)) {
|
262 |
| - $columnsChunks = array_chunk( |
263 |
| - $tableColumns, |
264 |
| - Action\Indexer::ATTRIBUTES_CHUNK_SIZE, |
265 |
| - true |
266 |
| - ); |
| 265 | + $columnsChunks = array_chunk($tableColumns, Action\Indexer::ATTRIBUTES_CHUNK_SIZE / 2, true); |
| 266 | + |
| 267 | + $entityTableName = $this->_productIndexerHelper->getTable('catalog_product_entity'); |
| 268 | + $entityTemporaryTableName = $this->_getTemporaryTableName($entityTableName); |
| 269 | + $temporaryTableName = $this->_getTemporaryTableName($tableName); |
| 270 | + $temporaryValueTableName = $temporaryTableName . $valueFieldSuffix; |
| 271 | + $attributeOptionValueTableName = $this->_productIndexerHelper->getTable('eav_attribute_option_value'); |
| 272 | + |
| 273 | + $flatColumns = $this->_productIndexerHelper->getFlatColumns(); |
| 274 | + $defaultStoreId = Store::DEFAULT_STORE_ID; |
| 275 | + $linkField = $this->getMetadataPool() |
| 276 | + ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class) |
| 277 | + ->getLinkField(); |
| 278 | + |
267 | 279 | foreach ($columnsChunks as $columnsList) {
|
268 | 280 | $select = $this->_connection->select();
|
269 | 281 | $selectValue = $this->_connection->select();
|
270 |
| - $entityTableName = $this->_getTemporaryTableName( |
271 |
| - $this->_productIndexerHelper->getTable('catalog_product_entity') |
272 |
| - ); |
273 |
| - $temporaryTableName = $this->_getTemporaryTableName($tableName); |
274 |
| - $temporaryValueTableName = $temporaryTableName . $valueFieldSuffix; |
275 |
| - $keyColumn = array_unique([$metadata->getLinkField(), 'entity_id']); |
| 282 | + $keyColumn = array_unique([$linkField, 'entity_id']); |
276 | 283 | $columns = array_merge($keyColumn, array_keys($columnsList));
|
277 | 284 | $valueColumns = $keyColumn;
|
278 |
| - $flatColumns = $this->_productIndexerHelper->getFlatColumns(); |
279 | 285 | $iterationNum = 1;
|
280 | 286 |
|
281 |
| - $select->from(['et' => $entityTableName], $keyColumn) |
282 |
| - ->join( |
283 |
| - ['e' => $this->resource->getTableName('catalog_product_entity')], |
284 |
| - 'e.entity_id = et.entity_id', |
285 |
| - [] |
286 |
| - ); |
| 287 | + $select->from(['et' => $entityTemporaryTableName], $keyColumn) |
| 288 | + ->join(['e' => $entityTableName], 'e.entity_id = et.entity_id', []); |
287 | 289 |
|
288 | 290 | $selectValue->from(['e' => $temporaryTableName], $keyColumn);
|
289 | 291 |
|
290 | 292 | /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
|
291 | 293 | foreach ($columnsList as $columnName => $attribute) {
|
292 | 294 | $countTableName = 't' . ($iterationNum++);
|
293 |
| - $joinCondition = sprintf( |
294 |
| - 'e.%3$s = %1$s.%3$s' . |
295 |
| - ' AND %1$s.attribute_id = %2$d' . |
296 |
| - ' AND (%1$s.store_id = %4$d' . |
297 |
| - ' OR %1$s.store_id = 0)', |
298 |
| - $countTableName, |
299 |
| - $attribute->getId(), |
300 |
| - $metadata->getLinkField(), |
301 |
| - $storeId |
302 |
| - ); |
303 |
| - |
| 295 | + $joinCondition = 'e.%3$s = %1$s.%3$s AND %1$s.attribute_id = %2$d AND %1$s.store_id = %4$d'; |
304 | 296 | $select->joinLeft(
|
305 | 297 | [$countTableName => $tableName],
|
306 |
| - $joinCondition, |
307 |
| - [$columnName => 'value'] |
| 298 | + sprintf($joinCondition, $countTableName, $attribute->getId(), $linkField, $defaultStoreId), |
| 299 | + [] |
| 300 | + )->joinLeft( |
| 301 | + ['s' . $countTableName => $tableName], |
| 302 | + sprintf($joinCondition, 's' . $countTableName, $attribute->getId(), $linkField, $storeId), |
| 303 | + [] |
308 | 304 | );
|
309 | 305 |
|
| 306 | + $columnValue = $this->_connection->getIfNullSql( |
| 307 | + 's' . $countTableName . '.value', |
| 308 | + $countTableName . '.value' |
| 309 | + ); |
| 310 | + $select->columns([$columnName => $columnValue]); |
| 311 | + |
310 | 312 | if ($attribute->getFlatUpdateSelect($storeId) instanceof \Magento\Framework\DB\Select) {
|
311 | 313 | $attributeCode = $attribute->getAttributeCode();
|
312 | 314 | $columnValueName = $attributeCode . $valueFieldSuffix;
|
313 | 315 | if (isset($flatColumns[$columnValueName])) {
|
314 |
| - $valueJoinCondition = sprintf( |
315 |
| - 'e.%1$s = %2$s.option_id AND (%2$s.store_id = %3$d OR %2$s.store_id = 0)', |
316 |
| - $attributeCode, |
317 |
| - $countTableName, |
318 |
| - $storeId |
319 |
| - ); |
| 316 | + $valueJoinCondition = 'e.%1$s = %2$s.option_id AND %2$s.store_id = %3$d'; |
320 | 317 | $selectValue->joinLeft(
|
321 |
| - [ |
322 |
| - $countTableName => $this->_productIndexerHelper->getTable( |
323 |
| - 'eav_attribute_option_value' |
324 |
| - ), |
325 |
| - ], |
326 |
| - $valueJoinCondition, |
327 |
| - [$columnValueName => $countTableName . '.value'] |
| 318 | + [$countTableName => $attributeOptionValueTableName], |
| 319 | + sprintf($valueJoinCondition, $attributeCode, $countTableName, $defaultStoreId), |
| 320 | + [] |
| 321 | + )->joinLeft( |
| 322 | + ['s' . $countTableName => $attributeOptionValueTableName], |
| 323 | + sprintf($valueJoinCondition, $attributeCode, 's' . $countTableName, $storeId), |
| 324 | + [] |
328 | 325 | );
|
| 326 | + |
| 327 | + $selectValue->columns([$columnValueName => $columnValue]); |
329 | 328 | $valueColumns[] = $columnValueName;
|
330 | 329 | }
|
331 | 330 | }
|
|
0 commit comments