Skip to content

Commit 622008c

Browse files
MAGETWO-69130: Non effective query for catalog search & layered navigation
1 parent c69f2a6 commit 622008c

File tree

11 files changed

+92
-140
lines changed

11 files changed

+92
-140
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock
1717
{
1818
/**
19-
* @var \Magento\Indexer\Model\ResourceModel\FrontendResource
19+
* @var \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher
2020
*/
21-
private $indexerStockFrontendResource;
21+
private $activeTableSwitcher;
2222

2323
/**
2424
* @var \Magento\Bundle\Model\ResourceModel\Indexer\StockStatusSelectBuilder
@@ -37,27 +37,25 @@ class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\
3737
* @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy
3838
* @param \Magento\Eav\Model\Config $eavConfig
3939
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
40-
* @param string $connectionName
41-
* @param null|\Magento\Indexer\Model\Indexer\StateFactory $stateFactory
42-
* @param null|\Magento\Indexer\Model\ResourceModel\FrontendResource $indexerStockFrontendResource
43-
* @param null|StockStatusSelectBuilder $stockStatusSelectBuilder
44-
* @param null|BundleOptionStockDataSelectBuilder $bundleOptionStockDataSelectBuilder
40+
* @param null $connectionName
41+
* @param \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher|null $activeTableSwitcher
42+
* @param StockStatusSelectBuilder|null $stockStatusSelectBuilder
43+
* @param BundleOptionStockDataSelectBuilder|null $bundleOptionStockDataSelectBuilder
4544
*/
4645
public function __construct(
4746
\Magento\Framework\Model\ResourceModel\Db\Context $context,
4847
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
4948
\Magento\Eav\Model\Config $eavConfig,
5049
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
5150
$connectionName = null,
52-
\Magento\Indexer\Model\Indexer\StateFactory $stateFactory = null,
53-
\Magento\Indexer\Model\ResourceModel\FrontendResource $indexerStockFrontendResource = null,
51+
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher = null,
5452
StockStatusSelectBuilder $stockStatusSelectBuilder = null,
5553
BundleOptionStockDataSelectBuilder $bundleOptionStockDataSelectBuilder = null
5654
) {
57-
parent::__construct($context, $tableStrategy, $eavConfig, $scopeConfig, $connectionName, $stateFactory);
55+
parent::__construct($context, $tableStrategy, $eavConfig, $scopeConfig, $connectionName);
5856

59-
$this->indexerStockFrontendResource = $indexerStockFrontendResource ?: ObjectManager::getInstance()
60-
->get(\Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\FrontendResource::class);
57+
$this->activeTableSwitcher = $activeTableSwitcher ?: ObjectManager::getInstance()
58+
->get(\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher::class);
6159

6260
$this->stockStatusSelectBuilder = $stockStatusSelectBuilder ?: ObjectManager::getInstance()
6361
->get(StockStatusSelectBuilder::class);
@@ -88,8 +86,8 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
8886
$this->_cleanBundleOptionStockData();
8987
$connection = $this->getConnection();
9088
$table = $this->getActionType() === Full::ACTION_TYPE
91-
? $this->getMainTable()
92-
: $this->indexerStockFrontendResource->getMainTable();
89+
? $this->activeTableSwitcher->getAdditionalTableName($this->getMainTable())
90+
: $this->getMainTable();
9391
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
9492
$select = $this->bundleOptionStockDataSelectBuilder->buildSelect($idxTable);
9593

app/code/Magento/CatalogInventory/Model/Indexer/Stock/AbstractAction.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -219,34 +219,6 @@ protected function _deleteOldRelations($tableName)
219219
$this->_getConnection()->query($sql);
220220
}
221221

222-
/**
223-
* Delete records by their ids from index table
224-
* Used to clean table before re-indexation
225-
*
226-
* @param array $ids
227-
* @return void
228-
*/
229-
protected function _deleteOldRecords(array $ids)
230-
{
231-
if (count($ids) !== 0) {
232-
$this->_getConnection()->delete(
233-
$this->indexerStockFrontendResource->getMainTable(),
234-
['product_id in (?)' => $ids]
235-
);
236-
}
237-
}
238-
239-
/**
240-
* Delete all records from index table
241-
* Used to clean table before re-indexation
242-
*
243-
* @return void
244-
*/
245-
protected function _cleanMainTable()
246-
{
247-
$this->_getConnection()->truncateTable($this->indexerStockFrontendResource->getMainTable());
248-
}
249-
250222
/**
251223
* Refresh entities index
252224
*
@@ -285,9 +257,6 @@ private function doReindex($productIds = [])
285257
->where('entity_id IN(?)', $processIds);
286258
$pairs = $connection->fetchPairs($select);
287259

288-
// delete current records from index
289-
$this->_deleteOldRecords($processIds);
290-
291260
$byType = [];
292261
foreach ($pairs as $productId => $typeId) {
293262
$byType[$typeId][$productId] = $productId;

app/code/Magento/CatalogInventory/Model/Indexer/Stock/Action/Full.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Framework\App\ObjectManager;
2121
use Magento\Framework\Exception\LocalizedException;
2222
use Magento\CatalogInventory\Model\Indexer\Stock\AbstractAction;
23+
use Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\StockInterface;
2324

2425
/**
2526
* Class Full reindex action
@@ -117,7 +118,7 @@ public function execute($ids = null)
117118
{
118119
try {
119120
$this->useIdxTable(false);
120-
$this->_cleanMainTable();
121+
$this->cleanIndexersTables($this->_getTypeIndexers());
121122

122123
$entityMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
123124

@@ -163,4 +164,26 @@ public function execute($ids = null)
163164
throw new LocalizedException(__($e->getMessage()), $e);
164165
}
165166
}
167+
168+
/**
169+
* Delete all records from index table
170+
* Used to clean table before re-indexation
171+
*
172+
* @return void
173+
*/
174+
private function cleanIndexersTables(array $indexers)
175+
{
176+
$tables = array_map(
177+
function(StockInterface $indexer) {
178+
return $this->activeTableSwitcher->getAdditionalTableName($indexer->getMainTable());
179+
},
180+
$indexers
181+
);
182+
183+
$tables = array_unique($tables);
184+
185+
foreach ($tables as $table) {
186+
$this->_getConnection()->truncateTable($table);
187+
}
188+
}
166189
}

app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,28 @@ protected function _updateIndex($entityIds)
305305
$data = [];
306306
}
307307
}
308+
309+
$this->deleteOldRecords($entityIds);
308310
$this->_updateIndexTable($data);
309311

310312
return $this;
311313
}
312314

315+
/**
316+
* Delete records by their ids from index table
317+
* Used to clean table before re-indexation
318+
*
319+
* @param array $ids
320+
* @return void
321+
* @throws LocalizedException
322+
*/
323+
private function deleteOldRecords(array $ids)
324+
{
325+
if (count($ids) !== 0) {
326+
$this->getConnection()->delete($this->getMainTable(), ['product_id in (?)' => $ids]);
327+
}
328+
}
329+
313330
/**
314331
* Update stock status index table (INSERT ... ON DUPLICATE KEY UPDATE ...)
315332
*

app/code/Magento/CatalogSearch/Model/Search/BaseSelectStrategy/BaseSelectAttributesSearchStrategy.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use Magento\Framework\App\ResourceConnection;
1010
use Magento\Store\Model\StoreManagerInterface;
11-
use Magento\Indexer\Model\ResourceModel\FrontendResource;
1211
use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;
1312
use Magento\CatalogSearch\Model\Search\SelectContainer\SelectContainer;
1413

@@ -31,11 +30,6 @@ class BaseSelectAttributesSearchStrategy implements BaseSelectStrategyInterface
3130
*/
3231
private $storeManager;
3332

34-
/**
35-
* @var FrontendResource
36-
*/
37-
private $indexerEavFrontendResource;
38-
3933
/**
4034
* @var IndexScopeResolver
4135
*/
@@ -44,18 +38,15 @@ class BaseSelectAttributesSearchStrategy implements BaseSelectStrategyInterface
4438
/**
4539
* @param ResourceConnection $resource
4640
* @param StoreManagerInterface $storeManager
47-
* @param FrontendResource $indexerEavFrontendResource
4841
* @param IndexScopeResolver $scopeResolver
4942
*/
5043
public function __construct(
5144
ResourceConnection $resource,
5245
StoreManagerInterface $storeManager,
53-
FrontendResource $indexerEavFrontendResource,
5446
IndexScopeResolver $scopeResolver
5547
) {
5648
$this->resource = $resource;
5749
$this->storeManager = $storeManager;
58-
$this->indexerEavFrontendResource = $indexerEavFrontendResource;
5950
$this->scopeResolver = $scopeResolver;
6051
}
6152

@@ -73,7 +64,7 @@ public function createBaseSelect(SelectContainer $selectContainer)
7364

7465
$select->distinct()
7566
->from(
76-
[$mainTableAlias => $this->indexerEavFrontendResource->getMainTable()],
67+
[$mainTableAlias => $this->resource->getTableName('catalog_product_index_eav')],
7768
['entity_id' => 'entity_id']
7869
)->where(
7970
$this->resource->getConnection()->quoteInto(

app/code/Magento/CatalogSearch/Model/Search/FilterMapper/CustomAttributeFilter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
namespace Magento\CatalogSearch\Model\Search\FilterMapper;
88

9+
use Magento\Framework\App\ResourceConnection;
910
use Magento\Framework\Search\Adapter\Mysql\ConditionManager;
1011
use Magento\Framework\DB\Select;
1112
use Magento\Eav\Model\Config as EavConfig;
1213
use Magento\Framework\Search\Request\FilterInterface;
13-
use Magento\Indexer\Model\ResourceModel\FrontendResource;
1414
use Magento\Store\Model\StoreManagerInterface;
1515
use Magento\CatalogSearch\Model\Adapter\Mysql\Filter\AliasResolver;
1616
use Magento\Catalog\Model\Product;
@@ -21,6 +21,11 @@
2121
*/
2222
class CustomAttributeFilter
2323
{
24+
/**
25+
* @var ResourceConnection
26+
*/
27+
private $resourceConnection;
28+
2429
/**
2530
* @var ConditionManager
2631
*/
@@ -36,33 +41,28 @@ class CustomAttributeFilter
3641
*/
3742
private $storeManager;
3843

39-
/**
40-
* @var FrontendResource
41-
*/
42-
private $indexerEavFrontendResource;
43-
4444
/**
4545
* @var AliasResolver
4646
*/
4747
private $aliasResolver;
4848

4949
/**
50+
* @param ResourceConnection $resourceConnection
5051
* @param ConditionManager $conditionManager
5152
* @param EavConfig $eavConfig
52-
* @param FrontendResource $indexerEavFrontendResource
5353
* @param StoreManagerInterface $storeManager
5454
* @param AliasResolver $aliasResolver
5555
*/
5656
public function __construct(
57+
ResourceConnection $resourceConnection,
5758
ConditionManager $conditionManager,
5859
EavConfig $eavConfig,
59-
FrontendResource $indexerEavFrontendResource,
6060
StoreManagerInterface $storeManager,
6161
AliasResolver $aliasResolver
6262
) {
63+
$this->resourceConnection = $resourceConnection;
6364
$this->conditionManager = $conditionManager;
6465
$this->eavConfig = $eavConfig;
65-
$this->indexerEavFrontendResource = $indexerEavFrontendResource;
6666
$this->storeManager = $storeManager;
6767
$this->aliasResolver = $aliasResolver;
6868
}
@@ -105,7 +105,7 @@ public function apply(Select $select, FilterInterface ... $filters)
105105
);
106106

107107
$select->joinInner(
108-
[$filterJoinAlias => $this->indexerEavFrontendResource->getMainTable()],
108+
[$filterJoinAlias => $this->resourceConnection->getTableName('catalog_product_index_eav')],
109109
$this->conditionManager->combineQueries($joinConditions, Select::SQL_AND),
110110
[]
111111
);
@@ -171,6 +171,7 @@ private function getAttributeIdByCode($field)
171171
*
172172
* @param Select $select
173173
* @return string|null
174+
* @throws \Zend_Db_Select_Exception
174175
*/
175176
private function extractTableAliasFromSelect(Select $select)
176177
{

app/code/Magento/CatalogSearch/Model/Search/FilterMapper/ExclusionStrategy.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
namespace Magento\CatalogSearch\Model\Search\FilterMapper;
88

99
use Magento\CatalogSearch\Model\Adapter\Mysql\Filter\AliasResolver;
10-
use Magento\Framework\App\ObjectManager;
11-
use Magento\Indexer\Model\ResourceModel\FrontendResource;
1210

1311
/**
1412
* Strategy which processes exclusions from general rules
@@ -30,16 +28,6 @@ class ExclusionStrategy implements FilterStrategyInterface
3028
*/
3129
private $storeManager;
3230

33-
/**
34-
* @var FrontendResource
35-
*/
36-
private $indexerFrontendResource;
37-
38-
/**
39-
* @var \Magento\Indexer\Model\ResourceModel\FrontendResource|null
40-
*/
41-
private $categoryProductIndexerFrontend;
42-
4331
/**
4432
* List of fields that can be processed by exclusion strategy
4533
* @var array
@@ -50,26 +38,15 @@ class ExclusionStrategy implements FilterStrategyInterface
5038
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
5139
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
5240
* @param AliasResolver $aliasResolver
53-
* @param FrontendResource $indexerFrontendResource
54-
* @param FrontendResource $categoryProductIndexerFrontend
55-
* @SuppressWarnings(Magento.TypeDuplication)
5641
*/
5742
public function __construct(
5843
\Magento\Framework\App\ResourceConnection $resourceConnection,
5944
\Magento\Store\Model\StoreManagerInterface $storeManager,
60-
AliasResolver $aliasResolver,
61-
FrontendResource $indexerFrontendResource = null,
62-
FrontendResource $categoryProductIndexerFrontend = null
45+
AliasResolver $aliasResolver
6346
) {
6447
$this->resourceConnection = $resourceConnection;
6548
$this->storeManager = $storeManager;
6649
$this->aliasResolver = $aliasResolver;
67-
$this->indexerFrontendResource = $indexerFrontendResource ?: ObjectManager::getInstance()->get(
68-
\Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\FrontendResource::class
69-
);
70-
$this->categoryProductIndexerFrontend = $categoryProductIndexerFrontend ?: ObjectManager::getInstance()->get(
71-
\Magento\Catalog\Model\ResourceModel\Product\Indexer\Category\Product\FrontendResource::class
72-
);
7350
}
7451

7552
/**
@@ -102,7 +79,7 @@ private function applyPriceFilter(
10279
\Magento\Framework\DB\Select $select
10380
) {
10481
$alias = $this->aliasResolver->getAlias($filter);
105-
$tableName = $this->indexerFrontendResource->getMainTable();
82+
$tableName = $this->resourceConnection->getTableName('catalog_product_index_price');
10683
$mainTableAlias = $this->extractTableAliasFromSelect($select);
10784

10885
$select->joinInner(
@@ -131,7 +108,7 @@ private function applyCategoryFilter(
131108
\Magento\Framework\DB\Select $select
132109
) {
133110
$alias = $this->aliasResolver->getAlias($filter);
134-
$tableName = $this->categoryProductIndexerFrontend->getMainTable();
111+
$tableName = $this->resourceConnection->getTableName('catalog_category_product_index');;
135112
$mainTableAlias = $this->extractTableAliasFromSelect($select);
136113

137114
$select->joinInner(

0 commit comments

Comments
 (0)