Skip to content

Commit 3345605

Browse files
committed
MC-21808: MySQL performance query optimization
1 parent 0dcb19e commit 3345605

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

app/code/Magento/Search/Model/PopularSearchTerms.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,24 @@ class PopularSearchTerms
2727
*/
2828
private $queryCollection;
2929

30+
/**
31+
* @var \Magento\Search\Model\ResourceModel\Query
32+
*/
33+
private $queryResource;
34+
3035
/**
3136
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
3237
* @param \Magento\Search\Model\ResourceModel\Query\Collection
38+
* @param ResourceModel\Query $queryResource
3339
*/
3440
public function __construct(
3541
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
36-
\Magento\Search\Model\ResourceModel\Query\Collection $queryCollection
42+
\Magento\Search\Model\ResourceModel\Query\Collection $queryCollection,
43+
\Magento\Search\Model\ResourceModel\Query $queryResource
3744
) {
3845
$this->scopeConfig = $scopeConfig;
3946
$this->queryCollection = $queryCollection;
47+
$this->queryResource = $queryResource;
4048
}
4149

4250
/**
@@ -48,13 +56,17 @@ public function __construct(
4856
*/
4957
public function isCacheable(string $term, int $storeId)
5058
{
51-
$terms = $this->queryCollection
52-
->setPopularQueryFilter($storeId)
53-
->setPageSize($this->getMaxCountCacheableSearchTerms($storeId))
54-
->load()
55-
->getColumnValues('query_text');
59+
$connection = $this->queryResource->getConnection();
60+
$select = $connection->select();
61+
$select->from($this->queryResource->getMainTable(), [$this->queryResource->getIdFieldName()])
62+
->where('query_text = ?', $term)
63+
->where('store_id = ?', $storeId)
64+
->where('num_results > 0')
65+
->order(['popularity DESC'])
66+
->limit($this->getMaxCountCacheableSearchTerms($storeId));
67+
$queryId = $connection->fetchOne($select);
5668

57-
return in_array($term, $terms);
69+
return (bool) $queryId;
5870
}
5971

6072
/**

0 commit comments

Comments
 (0)