Skip to content

Commit 0d4a6d0

Browse files
author
Oleksandr Iegorov
committed
MC-41647: SKU Search very slow on Admin Panel
1 parent f104c89 commit 0d4a6d0

File tree

1 file changed

+67
-17
lines changed
  • app/code/Magento/CatalogSearch/Model/ResourceModel/Search

1 file changed

+67
-17
lines changed

app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection implements
1818
\Magento\Search\Model\SearchCollectionInterface
1919
{
20+
private const INDEX_USAGE_ENFORCEMENTS = [
21+
'catalog_product_entity_text' => 'CATALOG_PRODUCT_ENTITY_TEXT_ROW_ID_ATTRIBUTE_ID_STORE_ID'
22+
];
23+
2024
/**
2125
* Attribute collection
2226
*
@@ -197,6 +201,21 @@ protected function _hasAttributeOptionsAndSearchable($attribute)
197201
return false;
198202
}
199203

204+
/**
205+
* Prepare table names for the index enforcements
206+
*
207+
* @return array
208+
*/
209+
private function prepareIndexEnforcements() : array
210+
{
211+
$result = [];
212+
foreach (self::INDEX_USAGE_ENFORCEMENTS as $table => $index) {
213+
$table = $this->getTable($table);
214+
$result[$table] = $index;
215+
}
216+
return $result;
217+
}
218+
200219
/**
201220
* Retrieve SQL for search entities
202221
*
@@ -208,6 +227,7 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr
208227
{
209228
$tables = [];
210229
$selects = [];
230+
$preparedIndexEnforcements = $this->prepareIndexEnforcements();
211231

212232
$likeOptions = ['position' => 'any'];
213233

@@ -249,23 +269,53 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr
249269

250270
$ifValueId = $this->getConnection()->getIfNullSql('t2.value', 't1.value');
251271
foreach ($tables as $table => $attributeIds) {
252-
$selects[] = $this->getConnection()->select()->from(
253-
['t1' => $table],
254-
$linkField
255-
)->joinLeft(
256-
['t2' => $table],
257-
$joinCondition,
258-
[]
259-
)->where(
260-
't1.attribute_id IN (?)',
261-
$attributeIds,
262-
\Zend_Db::INT_TYPE
263-
)->where(
264-
't1.store_id = ?',
265-
0
266-
)->where(
267-
$this->_resourceHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions)
268-
);
272+
if (!empty($preparedIndexEnforcements[$table])) {
273+
$condition1 = $this->_conn->quoteInto(
274+
'`t1`.`attribute_id` IN (?)',
275+
$attributeIds,
276+
\Zend_Db::INT_TYPE
277+
);
278+
$condition2 = '`t1`.`store_id` = 0';
279+
$condition3 = $this->_conn->quoteInto(
280+
'IFNULL(`t2`.`value`, `t1`.`value`) LIKE ?',
281+
$this->_resourceHelper->addLikeEscape($this->_searchQuery, $likeOptions)
282+
);
283+
284+
//force index statement not implemented in framework
285+
// phpcs:ignore Magento2.SQL.RawQuery
286+
$select = sprintf(
287+
'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s) LEFT JOIN `%s` AS `t2`
288+
ON %s WHERE %s AND %s AND %s',
289+
$linkField,
290+
$table,
291+
$preparedIndexEnforcements[$table],
292+
$table,
293+
$joinCondition,
294+
$condition1,
295+
$condition2,
296+
$condition3
297+
);
298+
} else {
299+
$select = $this->getConnection()->select();
300+
$select->from(
301+
['t1' => $table],
302+
$linkField
303+
)->joinLeft(
304+
['t2' => $table],
305+
$joinCondition,
306+
[]
307+
)->where(
308+
't1.attribute_id IN (?)',
309+
$attributeIds,
310+
\Zend_Db::INT_TYPE
311+
)->where(
312+
't1.store_id = ?',
313+
0
314+
)->where(
315+
$this->_resourceHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions)
316+
);
317+
}
318+
$selects[] = $select;
269319
}
270320

271321
$sql = $this->_getSearchInOptionSql($query);

0 commit comments

Comments
 (0)