Skip to content

Commit 03d5c35

Browse files
authored
Merge pull request #2804 from magento-performance/MAGETWO-92887
[performance] MAGETWO-92887: [Indexer optimizations] Tables sharding / segmentation for price indexer - part 2
2 parents ffa3df1 + 296e700 commit 03d5c35

File tree

14 files changed

+652
-260
lines changed

14 files changed

+652
-260
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,13 @@ protected function _prepareTierPriceIndex($entityIds = null)
260260
/**
261261
* Retrieve price indexers per product type
262262
*
263+
* @param bool $fullReindexAction
264+
*
263265
* @return \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\PriceInterface[]
264266
*
265267
* @throws \Magento\Framework\Exception\LocalizedException
266268
*/
267-
public function getTypeIndexers()
269+
public function getTypeIndexers($fullReindexAction = false)
268270
{
269271
if ($this->_indexers === null) {
270272
$this->_indexers = [];
@@ -275,7 +277,10 @@ public function getTypeIndexers()
275277
) ? $typeInfo['price_indexer'] : get_class($this->_defaultIndexerResource);
276278

277279
$indexer = $this->_indexerPriceFactory->create(
278-
$modelName
280+
$modelName,
281+
[
282+
'fullReindexAction' => $fullReindexAction
283+
]
279284
);
280285
// left setters for backward compatibility
281286
if ($indexer instanceof DefaultPrice) {

app/code/Magento/Catalog/Model/Indexer/Product/Price/Action/Full.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function execute($ids = null)
135135
$this->prepareTables();
136136

137137
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice $indexer */
138-
foreach ($this->getTypeIndexers() as $typeId => $priceIndexer) {
138+
foreach ($this->getTypeIndexers(true) as $typeId => $priceIndexer) {
139139
if ($priceIndexer instanceof DimensionalIndexerInterface) {
140140
//New price reindex mechanism
141141
$this->reindexProductTypeWithDimensions($priceIndexer, $typeId);
@@ -167,20 +167,7 @@ private function prepareTables()
167167

168168
$this->_prepareWebsiteDateTable();
169169

170-
$this->truncateReplicaTable();
171-
172-
$this->truncateReplicaTablesByDimensions();
173-
}
174-
175-
/**
176-
* Truncate main replica table
177-
*
178-
* @return void
179-
* @throws \Exception
180-
*/
181-
private function truncateReplicaTable()
182-
{
183-
$this->_defaultIndexerResource->getConnection()->truncateTable($this->getReplicaTable());
170+
$this->truncateReplicaTables();
184171
}
185172

186173
/**
@@ -189,7 +176,7 @@ private function truncateReplicaTable()
189176
* @return void
190177
* @throws \Exception
191178
*/
192-
private function truncateReplicaTablesByDimensions()
179+
private function truncateReplicaTables()
193180
{
194181
foreach ($this->dimensionCollectionFactory->create() as $dimension) {
195182
$dimensionTable = $this->dimensionTableMaintainer->getMainReplicaTable($dimension);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,10 @@ public function getMainReplicaTable(array $dimensions): string
251251
*/
252252
public function createMainTmpTable(array $dimensions)
253253
{
254-
$originTableName = $this->getMainTable($dimensions);
254+
// Create temporary table based on template table catalog_product_index_price_tmp without indexes
255+
$templateTableName = $this->resource->getTableName(self::MAIN_INDEX_TABLE . '_tmp');
255256
$temporaryTableName = $this->getMainTable($dimensions) . $this->tmpTableSuffix;
256-
$this->getConnection()->createTemporaryTableLike($temporaryTableName, $originTableName, true);
257+
$this->getConnection()->createTemporaryTableLike($temporaryTableName, $templateTableName, true);
257258
$this->mainTmpTable[$this->getArrayKeyForTmpTable($dimensions)] = $temporaryTableName;
258259
}
259260

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
9+
10+
/**
11+
* Apply price modifiers to product price indexer which are common for all product types:
12+
* custom options, catalog rule, catalog inventory modifiers
13+
*/
14+
class BasePriceModifier implements PriceModifierInterface
15+
{
16+
/**
17+
* @var PriceModifierInterface[]
18+
*/
19+
private $priceModifiers;
20+
21+
/**
22+
* @param array $priceModifiers
23+
*/
24+
public function __construct(array $priceModifiers)
25+
{
26+
$this->priceModifiers = $priceModifiers;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function modifyPrice(IndexTableStructure $priceTable, array $entityIds = [])
33+
{
34+
foreach ($this->priceModifiers as $priceModifier) {
35+
$priceModifier->modifyPrice($priceTable, $entityIds);
36+
}
37+
}
38+
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/SimpleProductPrice.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@ class SimpleProductPrice implements DimensionalIndexerInterface
3636
private $productType;
3737

3838
/**
39-
* @var PriceModifierInterface[]
39+
* @var BasePriceModifier
4040
*/
41-
private $priceModifiers;
41+
private $basePriceModifier;
4242

4343
/**
4444
* @param BaseFinalPrice $baseFinalPrice
4545
* @param IndexTableStructureFactory $indexTableStructureFactory
4646
* @param TableMaintainer $tableMaintainer
47+
* @param BasePriceModifier $basePriceModifier
4748
* @param string $productType
48-
* @param array $priceModifiers
4949
*/
5050
public function __construct(
5151
BaseFinalPrice $baseFinalPrice,
5252
IndexTableStructureFactory $indexTableStructureFactory,
5353
TableMaintainer $tableMaintainer,
54-
$productType = \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE,
55-
array $priceModifiers = []
54+
BasePriceModifier $basePriceModifier,
55+
$productType = \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
5656
) {
5757
$this->baseFinalPrice = $baseFinalPrice;
5858
$this->indexTableStructureFactory = $indexTableStructureFactory;
5959
$this->tableMaintainer = $tableMaintainer;
6060
$this->productType = $productType;
61-
$this->priceModifiers = $priceModifiers;
61+
$this->basePriceModifier = $basePriceModifier;
6262
}
6363

6464
/**
@@ -84,19 +84,6 @@ public function executeByDimension(array $dimensions, \Traversable $entityIds =
8484
$query = $select->insertFromSelect($temporaryPriceTable->getTableName(), [], false);
8585
$this->tableMaintainer->getConnection()->query($query);
8686

87-
$this->applyPriceModifiers($temporaryPriceTable);
88-
}
89-
90-
/**
91-
* Apply price modifiers to temporary price index table
92-
*
93-
* @param IndexTableStructure $temporaryPriceTable
94-
* @return void
95-
*/
96-
private function applyPriceModifiers(IndexTableStructure $temporaryPriceTable)
97-
{
98-
foreach ($this->priceModifiers as $priceModifier) {
99-
$priceModifier->modifyPrice($temporaryPriceTable);
100-
}
87+
$this->basePriceModifier->modifyPrice($temporaryPriceTable, iterator_to_array($entityIds));
10188
}
10289
}

app/code/Magento/Catalog/Setup/UpgradeSchema.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
144144

145145
if (version_compare($context->getVersion(), '2.2.6', '<')) {
146146
$this->addStoreIdFieldForWebsiteIndexTable($setup);
147+
$this->removeIndexFromPriceIndexTable($setup);
147148
}
148149

149150
$setup->endSetup();
@@ -819,4 +820,25 @@ private function addStoreIdFieldForWebsiteIndexTable(SchemaSetupInterface $setup
819820
]
820821
);
821822
}
823+
824+
/**
825+
* Table "catalog_product_index_price_tmp" used as template of "catalog_product_index_price" table
826+
* for create temporary tables during indexation. Indexes are removed from performance perspective
827+
* @param SchemaSetupInterface $setup
828+
*/
829+
private function removeIndexFromPriceIndexTable(SchemaSetupInterface $setup)
830+
{
831+
$setup->getConnection()->dropIndex(
832+
$setup->getTable('catalog_product_index_price_tmp'),
833+
$setup->getIdxName('catalog_product_index_price_tmp', ['customer_group_id'])
834+
);
835+
$setup->getConnection()->dropIndex(
836+
$setup->getTable('catalog_product_index_price_tmp'),
837+
$setup->getIdxName('catalog_product_index_price_tmp', ['website_id'])
838+
);
839+
$setup->getConnection()->dropIndex(
840+
$setup->getTable('catalog_product_index_price_tmp'),
841+
$setup->getIdxName('catalog_product_index_price_tmp', ['min_price'])
842+
);
843+
}
822844
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,15 +1088,9 @@
10881088
</type>
10891089
<type name="Magento\Catalog\Model\Indexer\Product\Price\Plugin\TableResolver">
10901090
<arguments>
1091-
<argument name="priceTableResolver" xsi:type="object">
1092-
Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver\Proxy
1093-
</argument>
1094-
<argument name="storeManager" xsi:type="object">
1095-
Magento\Store\Model\StoreManagerInterface\Proxy
1096-
</argument>
1097-
<argument name="context" xsi:type="object">
1098-
Magento\Framework\App\Http\Context\Proxy
1099-
</argument>
1091+
<argument name="priceTableResolver" xsi:type="object">Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver\Proxy</argument>
1092+
<argument name="storeManager" xsi:type="object">Magento\Store\Model\StoreManagerInterface\Proxy</argument>
1093+
<argument name="context" xsi:type="object">Magento\Framework\App\Http\Context\Proxy</argument>
11001094
<!-- Unccomment after fix issue with Proxy generation -->
11011095
<!--<argument name="dimensionModeConfiguration" xsi:type="object">-->
11021096
<!--Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration\Proxy-->
@@ -1121,7 +1115,7 @@
11211115
<argument name="tableResolver" xsi:type="object">Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver</argument>
11221116
</arguments>
11231117
</type>
1124-
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\SimpleProductPrice">
1118+
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier">
11251119
<arguments>
11261120
<argument name="priceModifiers" xsi:type="array">
11271121
<item name="customOptionPriceModifier" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CustomOptionPriceModifier</item>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
</argument>
126126
</arguments>
127127
</type>
128-
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\SimpleProductPrice">
128+
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier">
129129
<arguments>
130130
<argument name="priceModifiers" xsi:type="array">
131131
<item name="inventoryProductPriceIndexFilter" xsi:type="object">Magento\CatalogInventory\Model\Indexer\ProductPriceIndexFilter</item>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,13 @@
134134
</argument>
135135
</arguments>
136136
</type>
137-
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\SimpleProductPrice">
137+
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier">
138138
<arguments>
139139
<argument name="priceModifiers" xsi:type="array">
140140
<item name="catalogRulePriceModifier" xsi:type="object">Magento\CatalogRule\Model\Indexer\ProductPriceIndexModifier</item>
141141
</argument>
142142
</arguments>
143143
</type>
144-
145144
<virtualType name="CatalogRuleCustomConditionProvider" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor\ConditionProcessor\CustomConditionProvider">
146145
<arguments>
147146
<argument name="customConditionProcessors" xsi:type="array">

0 commit comments

Comments
 (0)