Skip to content

Commit 761b54a

Browse files
ENGCOM-7542: Improve performance of "in" condition on some version of MySQl #27129
2 parents 86b7f4b + 0ce4ac3 commit 761b54a

File tree

44 files changed

+509
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+509
-334
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Selection/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function joinPrices($websiteId)
215215
public function setOptionIdsFilter($optionIds)
216216
{
217217
if (!empty($optionIds)) {
218-
$this->getSelect()->where('selection.option_id IN (?)', $optionIds);
218+
$this->getSelect()->where('selection.option_id IN (?)', $optionIds, \Zend_Db::INT_TYPE);
219219
}
220220
return $this;
221221
}
@@ -229,7 +229,7 @@ public function setOptionIdsFilter($optionIds)
229229
public function setSelectionIdsFilter($selectionIds)
230230
{
231231
if (!empty($selectionIds)) {
232-
$this->getSelect()->where('selection.selection_id IN (?)', $selectionIds);
232+
$this->getSelect()->where('selection.selection_id IN (?)', $selectionIds, \Zend_Db::INT_TYPE);
233233
}
234234
return $this;
235235
}

app/code/Magento/BundleGraphQl/Model/Resolver/Links/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function fetch() : array
108108
}
109109

110110
$linkCollection->getSelect()
111-
->where($field . ' IN (?)', $this->parentIds);
111+
->where($field . ' IN (?)', $this->parentIds, \Zend_Db::INT_TYPE);
112112

113113
/** @var Selection $link */
114114
foreach ($linkCollection as $link) {

app/code/Magento/Catalog/Model/Indexer/Category/Flat/AbstractAction.php

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

77
namespace Magento\Catalog\Model\Indexer\Category\Flat;
88

9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\ResourceModel\Helper;
12+
use Magento\Framework\App\ObjectManager;
913
use Magento\Framework\App\ResourceConnection;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
15+
use Magento\Framework\DB\Ddl\Table;
16+
use Magento\Framework\EntityManager\EntityMetadata;
17+
use Magento\Store\Model\Store;
18+
use Magento\Store\Model\StoreManagerInterface;
1019

1120
/**
1221
* Abstract action class for category flat indexers.
22+
*
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1324
*/
1425
class AbstractAction
1526
{
@@ -31,14 +42,14 @@ class AbstractAction
3142
protected $resource;
3243

3344
/**
34-
* @var \Magento\Store\Model\StoreManagerInterface
45+
* @var StoreManagerInterface
3546
*/
3647
protected $storeManager;
3748

3849
/**
3950
* Catalog resource helper
4051
*
41-
* @var \Magento\Catalog\Model\ResourceModel\Helper
52+
* @var Helper
4253
*/
4354
protected $resourceHelper;
4455

@@ -50,12 +61,12 @@ class AbstractAction
5061
protected $columns = [];
5162

5263
/**
53-
* @var \Magento\Framework\DB\Adapter\AdapterInterface
64+
* @var AdapterInterface
5465
*/
5566
protected $connection;
5667

5768
/**
58-
* @var \Magento\Framework\EntityManager\EntityMetadata
69+
* @var EntityMetadata
5970
*/
6071
protected $categoryMetadata;
6172

@@ -68,13 +79,13 @@ class AbstractAction
6879

6980
/**
7081
* @param ResourceConnection $resource
71-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
72-
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
82+
* @param StoreManagerInterface $storeManager
83+
* @param Helper $resourceHelper
7384
*/
7485
public function __construct(
7586
ResourceConnection $resource,
76-
\Magento\Store\Model\StoreManagerInterface $storeManager,
77-
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
87+
StoreManagerInterface $storeManager,
88+
Helper $resourceHelper
7889
) {
7990
$this->resource = $resource;
8091
$this->connection = $resource->getConnection();
@@ -110,23 +121,22 @@ public function getColumns()
110121
* @param integer $storeId
111122
* @return string
112123
*/
113-
public function getMainStoreTable($storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID)
124+
public function getMainStoreTable($storeId = Store::DEFAULT_STORE_ID)
114125
{
115126
if (is_string($storeId)) {
116127
$storeId = (int) $storeId;
117128
}
118129

119130
$suffix = sprintf('store_%d', $storeId);
120-
$table = $this->connection->getTableName($this->getTableName('catalog_category_flat_' . $suffix));
121-
122-
return $table;
131+
return $this->connection->getTableName($this->getTableName('catalog_category_flat_' . $suffix));
123132
}
124133

125134
/**
126135
* Return structure for flat catalog table
127136
*
128137
* @param string $tableName
129-
* @return \Magento\Framework\DB\Ddl\Table
138+
* @return Table
139+
* @throws \Zend_Db_Exception
130140
*/
131141
protected function getFlatTableStructure($tableName)
132142
{
@@ -139,10 +149,10 @@ protected function getFlatTableStructure($tableName)
139149
//Adding columns
140150
foreach ($this->getColumns() as $fieldName => $fieldProp) {
141151
$default = $fieldProp['default'];
142-
if ($fieldProp['type'][0] == \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP
152+
if ($fieldProp['type'][0] == Table::TYPE_TIMESTAMP
143153
&& $default == 'CURRENT_TIMESTAMP'
144154
) {
145-
$default = \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT;
155+
$default = Table::TIMESTAMP_INIT;
146156
}
147157
$table->addColumn(
148158
$fieldName,
@@ -205,37 +215,37 @@ protected function getStaticColumns()
205215
$ddlType = $this->resourceHelper->getDdlTypeByColumnType($column['DATA_TYPE']);
206216
$column['DEFAULT'] = trim($column['DEFAULT'], "' ");
207217
switch ($ddlType) {
208-
case \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT:
209-
case \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER:
210-
case \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT:
218+
case Table::TYPE_SMALLINT:
219+
case Table::TYPE_INTEGER:
220+
case Table::TYPE_BIGINT:
211221
$isUnsigned = (bool)$column['UNSIGNED'];
212222
if ($column['DEFAULT'] === '') {
213223
$column['DEFAULT'] = null;
214224
}
215225

216226
$options = null;
217227
if ($column['SCALE'] > 0) {
218-
$ddlType = \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL;
228+
$ddlType = Table::TYPE_DECIMAL;
219229
} else {
220230
break;
221231
}
222232
// fall-through intentional
223-
case \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL:
233+
case Table::TYPE_DECIMAL:
224234
$options = $column['PRECISION'] . ',' . $column['SCALE'];
225235
$isUnsigned = null;
226236
if ($column['DEFAULT'] === '') {
227237
$column['DEFAULT'] = null;
228238
}
229239
break;
230-
case \Magento\Framework\DB\Ddl\Table::TYPE_TEXT:
240+
case Table::TYPE_TEXT:
231241
$options = $column['LENGTH'];
232242
$isUnsigned = null;
233243
break;
234-
case \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP:
244+
case Table::TYPE_TIMESTAMP:
235245
$options = null;
236246
$isUnsigned = null;
237247
break;
238-
case \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME:
248+
case Table::TYPE_DATETIME:
239249
$isUnsigned = null;
240250
break;
241251
}
@@ -248,7 +258,7 @@ protected function getStaticColumns()
248258
];
249259
}
250260
$columns['store_id'] = [
251-
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 5],
261+
'type' => [Table::TYPE_SMALLINT, 5],
252262
'unsigned' => true,
253263
'nullable' => false,
254264
'default' => '0',
@@ -274,7 +284,7 @@ protected function getEavColumns()
274284
switch ($attribute['backend_type']) {
275285
case 'varchar':
276286
$columns[$attribute['attribute_code']] = [
277-
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255],
287+
'type' => [Table::TYPE_TEXT, 255],
278288
'unsigned' => null,
279289
'nullable' => true,
280290
'default' => null,
@@ -283,7 +293,7 @@ protected function getEavColumns()
283293
break;
284294
case 'int':
285295
$columns[$attribute['attribute_code']] = [
286-
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null],
296+
'type' => [Table::TYPE_INTEGER, null],
287297
'unsigned' => null,
288298
'nullable' => true,
289299
'default' => null,
@@ -292,7 +302,7 @@ protected function getEavColumns()
292302
break;
293303
case 'text':
294304
$columns[$attribute['attribute_code']] = [
295-
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_TEXT, '64k'],
305+
'type' => [Table::TYPE_TEXT, '64k'],
296306
'unsigned' => null,
297307
'nullable' => true,
298308
'default' => null,
@@ -301,7 +311,7 @@ protected function getEavColumns()
301311
break;
302312
case 'datetime':
303313
$columns[$attribute['attribute_code']] = [
304-
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_DATETIME, null],
314+
'type' => [Table::TYPE_DATETIME, null],
305315
'unsigned' => null,
306316
'nullable' => true,
307317
'default' => null,
@@ -310,7 +320,7 @@ protected function getEavColumns()
310320
break;
311321
case 'decimal':
312322
$columns[$attribute['attribute_code']] = [
313-
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, '12,4'],
323+
'type' => [Table::TYPE_DECIMAL, '12,4'],
314324
'unsigned' => null,
315325
'nullable' => true,
316326
'default' => null,
@@ -346,7 +356,7 @@ protected function getAttributes()
346356
$this->connection->getTableName(
347357
$this->getTableName('eav_entity_type')
348358
) . '.entity_type_code = ?',
349-
\Magento\Catalog\Model\Category::ENTITY
359+
Category::ENTITY
350360
);
351361
$this->attributeCodes = [];
352362
foreach ($this->connection->fetchAll($select) as $attribute) {
@@ -414,7 +424,8 @@ private function getLinkIds(array $entityIds)
414424
[$linkField]
415425
)->where(
416426
'e.entity_id IN (?)',
417-
$entityIds
427+
$entityIds,
428+
\Zend_Db::INT_TYPE
418429
);
419430

420431
return $this->connection->fetchCol($select);
@@ -459,10 +470,12 @@ protected function getAttributeTypeValues($type, $entityIds, $storeId)
459470
]
460471
)->where(
461472
"e.entity_id IN (?)",
462-
$entityIds
473+
$entityIds,
474+
\Zend_Db::INT_TYPE
463475
)->where(
464476
'def.store_id IN (?)',
465-
[\Magento\Store\Model\Store::DEFAULT_STORE_ID, $storeId]
477+
[Store::DEFAULT_STORE_ID, $storeId],
478+
\Zend_Db::INT_TYPE
466479
);
467480

468481
return $this->connection->fetchAll($select);
@@ -501,14 +514,14 @@ protected function getTableName($name)
501514
/**
502515
* Get category metadata instance.
503516
*
504-
* @return \Magento\Framework\EntityManager\EntityMetadata
517+
* @return EntityMetadata
505518
*/
506519
private function getCategoryMetadata()
507520
{
508521
if (null === $this->categoryMetadata) {
509-
$metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
522+
$metadataPool = ObjectManager::getInstance()
510523
->get(\Magento\Framework\EntityManager\MetadataPool::class);
511-
$this->categoryMetadata = $metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class);
524+
$this->categoryMetadata = $metadataPool->getMetadata(CategoryInterface::class);
512525
}
513526
return $this->categoryMetadata;
514527
}
@@ -521,8 +534,8 @@ private function getCategoryMetadata()
521534
private function getSkipStaticColumns()
522535
{
523536
if (null === $this->skipStaticColumns) {
524-
$provider = \Magento\Framework\App\ObjectManager::getInstance()
525-
->get(\Magento\Catalog\Model\Indexer\Category\Flat\SkipStaticColumnsProvider::class);
537+
$provider = ObjectManager::getInstance()
538+
->get(SkipStaticColumnsProvider::class);
526539
$this->skipStaticColumns = $provider->get();
527540
}
528541
return $this->skipStaticColumns;

app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Rows.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ protected function filterIdsByStore(array $ids, $store)
119119
"path = {$rootIdExpr} OR path = {$rootCatIdExpr} OR path like {$catIdExpr}"
120120
)->where(
121121
"entity_id IN (?)",
122-
$ids
122+
$ids,
123+
\Zend_Db::INT_TYPE
123124
);
124125

125126
$resultIds = [];
@@ -170,27 +171,30 @@ private function buildIndexData(Store $store, $categoriesIdsChunk, $attributesDa
170171
foreach ($categoriesIdsChunk as $categoryId) {
171172
try {
172173
$category = $this->categoryRepository->get($categoryId);
173-
$categoryData = $category->getData();
174-
$linkId = $categoryData[$linkField];
175-
176-
$categoryAttributesData = [];
177-
if (isset($attributesData[$linkId]) && is_array($attributesData[$linkId])) {
178-
$categoryAttributesData = $attributesData[$linkId];
179-
}
180-
$categoryIndexData = $this->buildCategoryIndexData(
181-
$store,
182-
$categoryData,
183-
$categoryAttributesData
184-
);
185-
$data[] = $categoryIndexData;
186174
} catch (NoSuchEntityException $e) {
187-
// ignore
175+
continue;
188176
}
177+
178+
$categoryData = $category->getData();
179+
$linkId = $categoryData[$linkField];
180+
181+
$categoryAttributesData = [];
182+
if (isset($attributesData[$linkId]) && is_array($attributesData[$linkId])) {
183+
$categoryAttributesData = $attributesData[$linkId];
184+
}
185+
$categoryIndexData = $this->buildCategoryIndexData(
186+
$store,
187+
$categoryData,
188+
$categoryAttributesData
189+
);
190+
$data[] = $categoryIndexData;
189191
}
190192
return $data;
191193
}
192194

193195
/**
196+
* Prepare Category data
197+
*
194198
* @param Store $store
195199
* @param array $categoryData
196200
* @param array $categoryAttributesData
@@ -213,7 +217,8 @@ private function buildCategoryIndexData(Store $store, array $categoryData, array
213217
* Insert or update index data
214218
*
215219
* @param string $tableName
216-
* @param $data
220+
* @param array $data
221+
* @return void
217222
*/
218223
private function updateIndexData($tableName, $data)
219224
{

0 commit comments

Comments
 (0)