Skip to content

Commit f4bc33a

Browse files
committed
MAGETWO-64182: [Indexer optimizations] Batch data processing for price indexer
1 parent 3b5840b commit f4bc33a

File tree

5 files changed

+79
-50
lines changed

5 files changed

+79
-50
lines changed

app/code/Magento/Bundle/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
141141
<arguments>
142142
<argument name="batchRowsCount" xsi:type="array">
143-
<item name="bundle" xsi:type="number">100000</item>
143+
<item name="bundle" xsi:type="number">5000</item>
144144
</argument>
145145
<argument name="estimators" xsi:type="array">
146146
<item name="bundle" xsi:type="object">Magento\Catalog\Model\Indexer\Price\CompositeProductBatchSizeManagement</item>

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

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -228,61 +228,90 @@ protected function _prepareTierPriceIndex($entityIds = null)
228228
{
229229
$table = $this->_defaultIndexerResource->getTable('catalog_product_index_tier_price');
230230
$this->_emptyTable($table);
231-
232-
$tierPriceExpression = $this->_connection->getCheckSql(
233-
'tp.value = 0',
234-
'product_price.value * (1 - tp.percentage_value / 100)',
235-
'tp.value'
236-
);
237-
$websiteExpression = $this->_connection->getCheckSql(
238-
'tp.website_id = 0',
239-
'ROUND(' . $tierPriceExpression . ' * cwd.rate, 4)',
240-
$tierPriceExpression
241-
);
231+
if (empty($entityIds)) {
232+
return $this;
233+
}
242234
$linkField = $this->getProductIdFieldName();
243235
$priceAttribute = $this->getProductResource()->getAttribute('price');
244-
245-
$select = $this->_connection->select()->from(
236+
$baseColumns = [
237+
'cpe.entity_id',
238+
'tp.customer_group_id',
239+
'tp.website_id'
240+
];
241+
if ($linkField !== 'entity_id') {
242+
$baseColumns[] = 'cpe.' . $linkField;
243+
};
244+
$subSelect = $this->_connection->select()->from(
246245
['cpe' => $this->_defaultIndexerResource->getTable('catalog_product_entity')],
247-
['cpe.entity_id']
248-
)->join(
246+
array_merge_recursive(
247+
$baseColumns,
248+
[
249+
'min(tp.value) AS value',
250+
'min(tp.percentage_value) AS percentage_value'
251+
]
252+
)
253+
)->joinInner(
249254
['tp' => $this->_defaultIndexerResource->getTable(['catalog_product_entity', 'tier_price'])],
250255
'tp.' . $linkField . ' = cpe.' . $linkField,
251256
[]
252-
)->join(
253-
['cg' => $this->_defaultIndexerResource->getTable('customer_group')],
254-
'tp.all_groups = 1 OR (tp.all_groups = 0 AND tp.customer_group_id = cg.customer_group_id)',
255-
['customer_group_id']
256-
)->join(
257-
['cw' => $this->_defaultIndexerResource->getTable('store_website')],
258-
'tp.website_id = 0 OR tp.website_id = cw.website_id',
259-
['website_id']
260-
)->join(
261-
['cwd' => $this->_defaultIndexerResource->getTable('catalog_product_index_website')],
262-
'cw.website_id = cwd.website_id',
263-
[]
264-
)->join(
265-
['product_price' => $priceAttribute->getBackend()->getTable()],
266-
'tp.' . $linkField . ' = product_price.' . $linkField,
267-
[]
268-
)->where(
269-
'cw.website_id != 0'
270-
)->where(
271-
'product_price.attribute_id = ?',
272-
$priceAttribute->getAttributeId()
273-
)->columns(
274-
new \Zend_Db_Expr("MIN({$websiteExpression})")
275-
)->group(
276-
['cpe.entity_id', 'cg.customer_group_id', 'cw.website_id']
277-
);
257+
)->where("cpe.entity_id IN(?)", $entityIds)
258+
->where("tp.website_id != 0")
259+
->group(['cpe.entity_id', 'tp.customer_group_id', 'tp.website_id']);
260+
261+
$subSelect2 = $this->_connection->select()
262+
->from(
263+
['cpe' => $this->_defaultIndexerResource->getTable('catalog_product_entity')],
264+
array_merge_recursive(
265+
$baseColumns,
266+
[
267+
'MIN(ROUND(tp.value * cwd.rate, 4)) AS value',
268+
'MIN(ROUND(tp.percentage_value * cwd.rate, 4)) AS percentage_value'
269+
270+
]
271+
)
272+
)
273+
->joinInner(
274+
['tp' => $this->_defaultIndexerResource->getTable(['catalog_product_entity', 'tier_price'])],
275+
'tp.' . $linkField . ' = cpe.' . $linkField,
276+
[]
277+
)->join(
278+
['cw' => $this->_defaultIndexerResource->getTable('store_website')],
279+
true,
280+
[]
281+
)
282+
->joinInner(
283+
['cwd' => $this->_defaultIndexerResource->getTable('catalog_product_index_website')],
284+
'cw.website_id = cwd.website_id',
285+
[]
286+
)
287+
->where("cpe.entity_id IN(?)", $entityIds)
288+
->where("tp.website_id = 0")
289+
->group(
290+
['cpe.entity_id', 'tp.customer_group_id', 'tp.website_id']
291+
);
278292

279-
if (!empty($entityIds)) {
280-
$select->where("cpe.entity_id IN(?)", $entityIds);
281-
}
293+
$unionSelect = $this->_connection->select()
294+
->union([$subSelect, $subSelect2], \Magento\Framework\DB\Select::SQL_UNION_ALL);
295+
$select = $this->_connection->select()
296+
->from(
297+
['b' => new \Zend_Db_Expr(sprintf('(%s)', $unionSelect->assemble()))],
298+
[
299+
'b.entity_id',
300+
'b.customer_group_id',
301+
'b.website_id',
302+
'MIN(IF(b.value = 0, product_price.value * (1 - b.percentage_value / 100), b.value))'
303+
]
304+
)
305+
->joinInner(
306+
['product_price' => $priceAttribute->getBackend()->getTable()],
307+
'b.' . $linkField . ' = product_price.' . $linkField,
308+
[]
309+
)
310+
->group(['b.entity_id', 'b.customer_group_id', 'b.website_id']);
311+
312+
$query = $select->insertFromSelect($table, [], false);
282313

283-
$query = $select->insertFromSelect($table);
284314
$this->_connection->query($query);
285-
286315
return $this;
287316
}
288317

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@
918918
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
919919
<arguments>
920920
<argument name="batchRowsCount" xsi:type="array">
921-
<item name="default" xsi:type="number">200000</item>
921+
<item name="default" xsi:type="number">5000</item>
922922
</argument>
923923
<argument name="estimators" xsi:type="array">
924924
<item name="default" xsi:type="object">Magento\Catalog\Model\Indexer\Price\BatchSizeManagement</item>

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
175175
<arguments>
176176
<argument name="batchRowsCount" xsi:type="array">
177-
<item name="configurable" xsi:type="number">100000</item>
177+
<item name="configurable" xsi:type="number">5000</item>
178178
</argument>
179179
<argument name="estimators" xsi:type="array">
180180
<item name="configurable" xsi:type="object">Magento\Catalog\Model\Indexer\Price\CompositeProductBatchSizeManagement</item>

app/code/Magento/GroupedProduct/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
9393
<arguments>
9494
<argument name="batchRowsCount" xsi:type="array">
95-
<item name="grouped" xsi:type="number">100000</item>
95+
<item name="grouped" xsi:type="number">5000</item>
9696
</argument>
9797
<argument name="estimators" xsi:type="array">
9898
<item name="grouped" xsi:type="object">Magento\Catalog\Model\Indexer\Price\CompositeProductBatchSizeManagement</item>

0 commit comments

Comments
 (0)