Skip to content

Commit b3860bc

Browse files
committed
MC-21808: MySQL performance query optimization
1 parent 9b6cfdf commit b3860bc

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

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

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

30-
/**
31-
* @var \Magento\Search\Model\ResourceModel\Query
32-
*/
33-
private $queryResource;
34-
3530
/**
3631
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
3732
* @param \Magento\Search\Model\ResourceModel\Query\Collection
38-
* @param ResourceModel\Query $queryResource
3933
*/
4034
public function __construct(
4135
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
42-
\Magento\Search\Model\ResourceModel\Query\Collection $queryCollection,
43-
\Magento\Search\Model\ResourceModel\Query $queryResource
36+
\Magento\Search\Model\ResourceModel\Query\Collection $queryCollection
4437
) {
4538
$this->scopeConfig = $scopeConfig;
4639
$this->queryCollection = $queryCollection;
47-
$this->queryResource = $queryResource;
4840
}
4941

5042
/**
@@ -56,17 +48,13 @@ public function __construct(
5648
*/
5749
public function isCacheable(string $term, int $storeId)
5850
{
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);
51+
$terms = $this->queryCollection
52+
->setPopularQueryFilter($storeId)
53+
->setPageSize($this->getMaxCountCacheableSearchTerms($storeId))
54+
->load()
55+
->getColumnValues('query_text');
6856

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

7260
/**

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public function setQueryFilter($query)
130130
*/
131131
public function setPopularQueryFilter($storeIds = null)
132132
{
133-
134133
$this->getSelect()->reset(
135134
\Magento\Framework\DB\Select::FROM
136135
)->reset(
@@ -140,13 +139,10 @@ public function setPopularQueryFilter($storeIds = null)
140139
)->from(
141140
['main_table' => $this->getTable('search_query')]
142141
);
143-
if ($storeIds) {
144-
$this->addStoreFilter($storeIds);
145-
$this->getSelect()->where('num_results > 0');
146-
} elseif (null === $storeIds) {
147-
$this->addStoreFilter($this->_storeManager->getStore()->getId());
148-
$this->getSelect()->where('num_results > 0');
149-
}
142+
143+
$storeIds = $storeIds ?: $this->_storeManager->getStore()->getId();
144+
$this->addStoreFilter($storeIds);
145+
$this->getSelect()->where('num_results > 0');
150146

151147
$this->getSelect()->order(['popularity desc']);
152148

@@ -172,10 +168,9 @@ public function setRecentQueryFilter()
172168
*/
173169
public function addStoreFilter($storeIds)
174170
{
175-
if (!is_array($storeIds)) {
176-
$storeIds = [$storeIds];
177-
}
178-
$this->getSelect()->where('main_table.store_id IN (?)', $storeIds);
171+
$condition = is_array($storeIds) ? 'main_table.store_id IN (?)' : 'main_table.store_id = ?';
172+
$this->getSelect()->where($condition, $storeIds);
173+
179174
return $this;
180175
}
181176
}

app/code/Magento/Search/etc/db_schema.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<index referenceId="SEARCH_QUERY_IS_PROCESSED" indexType="btree">
4747
<column name="is_processed"/>
4848
</index>
49+
<index referenceId="SEARCH_QUERY_STORE_ID_POPULARITY" indexType="btree">
50+
<column name="store_id"/>
51+
<column name="popularity"/>
52+
</index>
4953
</table>
5054
<table name="search_synonyms" resource="default" engine="innodb" comment="table storing various synonyms groups">
5155
<column xsi:type="bigint" name="group_id" padding="20" unsigned="true" nullable="false" identity="true"

0 commit comments

Comments
 (0)