Skip to content

Commit bc287ff

Browse files
MAGETWO-69130: Non effective query for catalog search & layered navigation
1 parent e6394e6 commit bc287ff

File tree

14 files changed

+107
-107
lines changed

14 files changed

+107
-107
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
use Magento\Catalog\Api\Data\ProductInterface;
1010
use Magento\Framework\DB\Select;
1111

12+
/**
13+
* Class BundleOptionStockDataSelectBuilder
14+
* Is used to create Select object that is used for Bundle product stock status indexation
15+
*
16+
* @see \Magento\Bundle\Model\ResourceModel\Indexer\Stock::_prepareBundleOptionStockData
17+
*/
1218
class BundleOptionStockDataSelectBuilder
1319
{
1420
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
1212

13+
/**
14+
* Class StockStatusSelectBuilder
15+
* Is used to create Select object that is used for Bundle product stock status indexation
16+
*
17+
* @see \Magento\Bundle\Model\ResourceModel\Indexer\Stock::_getStockStatusSelect
18+
*/
1319
class StockStatusSelectBuilder
1420
{
1521

app/code/Magento/CatalogSearch/Model/Search/CustomAttributeFilterCheck.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function isCustom(FilterInterface $filter)
4646
}
4747

4848
/**
49+
* Return attribute by its code
50+
*
4951
* @param string $field
5052
* @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute
5153
* @throws \Magento\Framework\Exception\LocalizedException

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,19 @@ public function apply(Select $select, FilterInterface ... $filters)
9696

9797
$attributes[] = $attributeId;
9898

99-
$joinConditions = $this->createQueryConditions($attributeId, $filterJoinAlias);
100-
101-
array_unshift(
102-
$joinConditions,
103-
sprintf('%s.entity_id = %s.entity_id', $mainTableAlias, $filterJoinAlias),
104-
sprintf('%s.source_id = %s.source_id', $mainTableAlias, $filterJoinAlias)
105-
);
106-
10799
$select->joinInner(
108100
[$filterJoinAlias => $this->resourceConnection->getTableName('catalog_product_index_eav')],
109-
$this->conditionManager->combineQueries($joinConditions, Select::SQL_AND),
101+
$this->conditionManager->combineQueries(
102+
$this->getJoinConditions($attributeId, $mainTableAlias, $filterJoinAlias),
103+
Select::SQL_AND
104+
),
110105
[]
111106
);
112107
}
113108

114109
if (count($attributes) === 1) {
115110
// forces usage of PRIMARY key in main table
111+
// is required to boost performance in case when we have just one filter by custom attribute
116112
$attribute = reset($attributes);
117113
$filter = reset($filters);
118114
$select->where(
@@ -134,27 +130,34 @@ public function apply(Select $select, FilterInterface ... $filters)
134130
}
135131

136132
/**
137-
* @param int $attributeId
138-
* @param string $mainTableAlias
133+
* Returns Joins conditions for table catalog_product_index_eav
134+
*
135+
* @param int $attrId
136+
* @param string $mainTable
137+
* @param string $joinTable
139138
* @return array
140139
*/
141-
private function createQueryConditions($attributeId, $mainTableAlias)
140+
private function getJoinConditions($attrId, $mainTable, $joinTable)
142141
{
143142
return [
143+
sprintf('`%s`.`entity_id` = `%s`.`entity_id`', $mainTable, $joinTable),
144+
sprintf('`%s`.`source_id` = `%s`.`source_id`', $mainTable, $joinTable),
144145
$this->conditionManager->generateCondition(
145-
sprintf('%s.attribute_id', $mainTableAlias),
146+
sprintf('%s.attribute_id', $joinTable),
146147
'=',
147-
$attributeId
148+
$attrId
148149
),
149150
$this->conditionManager->generateCondition(
150-
sprintf('%s.store_id', $mainTableAlias),
151+
sprintf('%s.store_id', $joinTable),
151152
'=',
152153
(int) $this->storeManager->getStore()->getId()
153154
)
154155
];
155156
}
156157

157158
/**
159+
* Returns attribute id by code
160+
*
158161
* @param string $field
159162
* @return int|null
160163
* @throws \Magento\Framework\Exception\LocalizedException

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function processDimensions(SelectContainer $selectContainer)
6464
}
6565

6666
/**
67+
* Prepares where conditions from dimensions
68+
*
6769
* @param Dimension[] $dimensions
6870
* @return string[]
6971
*/

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function apply(
5656
\Magento\Framework\Search\Request\FilterInterface $filter,
5757
\Magento\Framework\DB\Select $select
5858
) {
59-
if (!in_array($filter->getField(), $this->validFields)) {
59+
if (!in_array($filter->getField(), $this->validFields, true)) {
6060
return false;
6161
}
6262

@@ -68,6 +68,8 @@ public function apply(
6868
}
6969

7070
/**
71+
* Applies filter bt price field
72+
*
7173
* @param \Magento\Framework\Search\Request\FilterInterface $filter
7274
* @param \Magento\Framework\DB\Select $select
7375
* @return bool
@@ -97,6 +99,8 @@ private function applyPriceFilter(
9799
}
98100

99101
/**
102+
* Applies filter by category
103+
*
100104
* @param \Magento\Framework\Search\Request\FilterInterface $filter
101105
* @param \Magento\Framework\DB\Select $select
102106
* @return bool

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use Magento\CatalogSearch\Model\Search\SelectContainer\SelectContainer;
1010
use Magento\CatalogSearch\Model\Adapter\Mysql\Filter\AliasResolver;
11-
use Magento\CatalogSearch\Model\Search\FilterMapper\StockStatusFilter;
1211
use Magento\CatalogInventory\Model\Stock;
1312

1413
/**
@@ -96,7 +95,6 @@ public function applyFilters(SelectContainer $selectContainer)
9695
$appliedFilters = [];
9796

9897
if ($selectContainer->hasVisibilityFilter()) {
99-
10098
$filterType = VisibilityFilter::FILTER_BY_WHERE;
10199
if ($selectContainer->hasCustomAttributesFilters()) {
102100
$filterType = VisibilityFilter::FILTER_BY_JOIN;
@@ -120,19 +118,4 @@ public function applyFilters(SelectContainer $selectContainer)
120118
$selectContainer = $selectContainer->updateSelect($select);
121119
return $selectContainer;
122120
}
123-
124-
/**
125-
* This method is deprecated.
126-
* Please use \Magento\CatalogSearch\Model\Adapter\Mysql\Filter\AliasResolver::getAlias() instead.
127-
*
128-
* @deprecated
129-
* @see AliasResolver::getAlias()
130-
*
131-
* @param \Magento\Framework\Search\Request\FilterInterface $filter
132-
* @return string
133-
*/
134-
public function getMappingAlias(\Magento\Framework\Search\Request\FilterInterface $filter)
135-
{
136-
return $this->aliasResolver->getAlias($filter);
137-
}
138121
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ class StockStatusFilter
2424
private $resourceConnection;
2525

2626
/**
27-
* Defines strategies of how filter should be applied
27+
* Defines strategy of how filter should be applied
28+
*
29+
* Stock status filter will be applied only on parent products
30+
* (e.g. only for configurable products, without options)
2831
*/
2932
const FILTER_JUST_ENTITY = 'general_filter';
33+
34+
/**
35+
* Defines strategy of how filter should be applied
36+
*
37+
* Stock status filter will be applied on parent products with its child
38+
* (e.g. for configurable products and options)
39+
*/
3040
const FILTER_ENTITY_AND_SUB_PRODUCTS = 'filter_with_sub_products';
3141

3242
/**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ class VisibilityFilter
2626

2727
/**
2828
* Defines strategies of how filter should be applied
29+
*
30+
* Visibility filter will be applied through the additional inner join condition
31+
* This strategy is used when general Select query contains filters by other eav attributes
2932
*/
3033
const FILTER_BY_JOIN = 'join_filter';
34+
35+
/**
36+
* Defines strategies of how filter should be applied
37+
*
38+
* Visibility filter will be applied through the additional where condition
39+
* This strategy is used when general Select query does not contain any other filters by eav attributes
40+
*/
3141
const FILTER_BY_WHERE = 'where_filter';
3242

3343
/**
@@ -171,6 +181,8 @@ private function applyFilterByWhere(FilterInterface $filter, Select $select)
171181
}
172182

173183
/**
184+
* Returns visibility attribute id
185+
*
174186
* @return int
175187
* @throws \Magento\Framework\Exception\LocalizedException
176188
* @throws \InvalidArgumentException
@@ -183,7 +195,7 @@ private function getVisibilityAttributeId()
183195
);
184196

185197
if ($attr === null) {
186-
throw new \InvalidArgumentException('Wrong id for visibility attribute');
198+
throw new \InvalidArgumentException('Wrong code for visibility attribute');
187199
}
188200

189201
return (int) $attr->getId();

app/code/Magento/CatalogSearch/Model/Search/SelectContainer/SelectContainer.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ class SelectContainer
5656
private $dimensions;
5757

5858
/**
59+
* @param Select $select
5960
* @param array $nonCustomAttributesFilters
6061
* @param array $customAttributesFilters
61-
* @param FilterInterface $visibilityFilter
62+
* @param array $dimensions
6263
* @param bool $isFullTextSearchRequired
6364
* @param bool $isShowOutOfStockEnabled
64-
* @param Select $select
6565
* @param string $usedIndex
66-
* @param array $dimensions
66+
* @param FilterInterface|null $visibilityFilter
6767
*/
6868
public function __construct(
69+
Select $select,
6970
array $nonCustomAttributesFilters,
7071
array $customAttributesFilters,
71-
$visibilityFilter,
72+
array $dimensions,
7273
bool $isFullTextSearchRequired,
7374
bool $isShowOutOfStockEnabled,
7475
$usedIndex,
75-
array $dimensions,
76-
Select $select
76+
FilterInterface $visibilityFilter = null
7777
) {
7878
$this->nonCustomAttributesFilters = $nonCustomAttributesFilters;
7979
$this->customAttributesFilters = $customAttributesFilters;
@@ -180,15 +180,21 @@ public function getSelect()
180180
*/
181181
public function updateSelect(Select $select)
182182
{
183-
return new self(
183+
$data = [
184+
clone $select,
184185
$this->nonCustomAttributesFilters,
185186
$this->customAttributesFilters,
186-
$this->visibilityFilter,
187+
$this->dimensions,
187188
$this->isFullTextSearchRequired,
188189
$this->isShowOutOfStockEnabled,
189-
$this->usedIndex,
190-
$this->dimensions,
191-
clone $select
192-
);
190+
$this->usedIndex
191+
192+
];
193+
194+
if ($this->visibilityFilter !== null) {
195+
$data[] = clone $this->visibilityFilter;
196+
}
197+
198+
return new self(...$data);
193199
}
194200
}

0 commit comments

Comments
 (0)