Skip to content

Commit

Permalink
ENGCOM-7542: Improve performance of "in" condition on some version of…
Browse files Browse the repository at this point in the history
… MySQl #27129
  • Loading branch information
gabrieldagama authored Sep 3, 2020
2 parents 86b7f4b + 0ce4ac3 commit 761b54a
Show file tree
Hide file tree
Showing 44 changed files with 509 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function joinPrices($websiteId)
public function setOptionIdsFilter($optionIds)
{
if (!empty($optionIds)) {
$this->getSelect()->where('selection.option_id IN (?)', $optionIds);
$this->getSelect()->where('selection.option_id IN (?)', $optionIds, \Zend_Db::INT_TYPE);
}
return $this;
}
Expand All @@ -229,7 +229,7 @@ public function setOptionIdsFilter($optionIds)
public function setSelectionIdsFilter($selectionIds)
{
if (!empty($selectionIds)) {
$this->getSelect()->where('selection.selection_id IN (?)', $selectionIds);
$this->getSelect()->where('selection.selection_id IN (?)', $selectionIds, \Zend_Db::INT_TYPE);
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function fetch() : array
}

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

/** @var Selection $link */
foreach ($linkCollection as $link) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@

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

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Helper;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\EntityManager\EntityMetadata;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* Abstract action class for category flat indexers.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractAction
{
Expand All @@ -31,14 +42,14 @@ class AbstractAction
protected $resource;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* Catalog resource helper
*
* @var \Magento\Catalog\Model\ResourceModel\Helper
* @var Helper
*/
protected $resourceHelper;

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

/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
* @var AdapterInterface
*/
protected $connection;

/**
* @var \Magento\Framework\EntityManager\EntityMetadata
* @var EntityMetadata
*/
protected $categoryMetadata;

Expand All @@ -68,13 +79,13 @@ class AbstractAction

/**
* @param ResourceConnection $resource
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
* @param StoreManagerInterface $storeManager
* @param Helper $resourceHelper
*/
public function __construct(
ResourceConnection $resource,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
StoreManagerInterface $storeManager,
Helper $resourceHelper
) {
$this->resource = $resource;
$this->connection = $resource->getConnection();
Expand Down Expand Up @@ -110,23 +121,22 @@ public function getColumns()
* @param integer $storeId
* @return string
*/
public function getMainStoreTable($storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID)
public function getMainStoreTable($storeId = Store::DEFAULT_STORE_ID)
{
if (is_string($storeId)) {
$storeId = (int) $storeId;
}

$suffix = sprintf('store_%d', $storeId);
$table = $this->connection->getTableName($this->getTableName('catalog_category_flat_' . $suffix));

return $table;
return $this->connection->getTableName($this->getTableName('catalog_category_flat_' . $suffix));
}

/**
* Return structure for flat catalog table
*
* @param string $tableName
* @return \Magento\Framework\DB\Ddl\Table
* @return Table
* @throws \Zend_Db_Exception
*/
protected function getFlatTableStructure($tableName)
{
Expand All @@ -139,10 +149,10 @@ protected function getFlatTableStructure($tableName)
//Adding columns
foreach ($this->getColumns() as $fieldName => $fieldProp) {
$default = $fieldProp['default'];
if ($fieldProp['type'][0] == \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP
if ($fieldProp['type'][0] == Table::TYPE_TIMESTAMP
&& $default == 'CURRENT_TIMESTAMP'
) {
$default = \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT;
$default = Table::TIMESTAMP_INIT;
}
$table->addColumn(
$fieldName,
Expand Down Expand Up @@ -205,37 +215,37 @@ protected function getStaticColumns()
$ddlType = $this->resourceHelper->getDdlTypeByColumnType($column['DATA_TYPE']);
$column['DEFAULT'] = trim($column['DEFAULT'], "' ");
switch ($ddlType) {
case \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT:
case \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER:
case \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT:
case Table::TYPE_SMALLINT:
case Table::TYPE_INTEGER:
case Table::TYPE_BIGINT:
$isUnsigned = (bool)$column['UNSIGNED'];
if ($column['DEFAULT'] === '') {
$column['DEFAULT'] = null;
}

$options = null;
if ($column['SCALE'] > 0) {
$ddlType = \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL;
$ddlType = Table::TYPE_DECIMAL;
} else {
break;
}
// fall-through intentional
case \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL:
case Table::TYPE_DECIMAL:
$options = $column['PRECISION'] . ',' . $column['SCALE'];
$isUnsigned = null;
if ($column['DEFAULT'] === '') {
$column['DEFAULT'] = null;
}
break;
case \Magento\Framework\DB\Ddl\Table::TYPE_TEXT:
case Table::TYPE_TEXT:
$options = $column['LENGTH'];
$isUnsigned = null;
break;
case \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP:
case Table::TYPE_TIMESTAMP:
$options = null;
$isUnsigned = null;
break;
case \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME:
case Table::TYPE_DATETIME:
$isUnsigned = null;
break;
}
Expand All @@ -248,7 +258,7 @@ protected function getStaticColumns()
];
}
$columns['store_id'] = [
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 5],
'type' => [Table::TYPE_SMALLINT, 5],
'unsigned' => true,
'nullable' => false,
'default' => '0',
Expand All @@ -274,7 +284,7 @@ protected function getEavColumns()
switch ($attribute['backend_type']) {
case 'varchar':
$columns[$attribute['attribute_code']] = [
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255],
'type' => [Table::TYPE_TEXT, 255],
'unsigned' => null,
'nullable' => true,
'default' => null,
Expand All @@ -283,7 +293,7 @@ protected function getEavColumns()
break;
case 'int':
$columns[$attribute['attribute_code']] = [
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null],
'type' => [Table::TYPE_INTEGER, null],
'unsigned' => null,
'nullable' => true,
'default' => null,
Expand All @@ -292,7 +302,7 @@ protected function getEavColumns()
break;
case 'text':
$columns[$attribute['attribute_code']] = [
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_TEXT, '64k'],
'type' => [Table::TYPE_TEXT, '64k'],
'unsigned' => null,
'nullable' => true,
'default' => null,
Expand All @@ -301,7 +311,7 @@ protected function getEavColumns()
break;
case 'datetime':
$columns[$attribute['attribute_code']] = [
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_DATETIME, null],
'type' => [Table::TYPE_DATETIME, null],
'unsigned' => null,
'nullable' => true,
'default' => null,
Expand All @@ -310,7 +320,7 @@ protected function getEavColumns()
break;
case 'decimal':
$columns[$attribute['attribute_code']] = [
'type' => [\Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, '12,4'],
'type' => [Table::TYPE_DECIMAL, '12,4'],
'unsigned' => null,
'nullable' => true,
'default' => null,
Expand Down Expand Up @@ -346,7 +356,7 @@ protected function getAttributes()
$this->connection->getTableName(
$this->getTableName('eav_entity_type')
) . '.entity_type_code = ?',
\Magento\Catalog\Model\Category::ENTITY
Category::ENTITY
);
$this->attributeCodes = [];
foreach ($this->connection->fetchAll($select) as $attribute) {
Expand Down Expand Up @@ -414,7 +424,8 @@ private function getLinkIds(array $entityIds)
[$linkField]
)->where(
'e.entity_id IN (?)',
$entityIds
$entityIds,
\Zend_Db::INT_TYPE
);

return $this->connection->fetchCol($select);
Expand Down Expand Up @@ -459,10 +470,12 @@ protected function getAttributeTypeValues($type, $entityIds, $storeId)
]
)->where(
"e.entity_id IN (?)",
$entityIds
$entityIds,
\Zend_Db::INT_TYPE
)->where(
'def.store_id IN (?)',
[\Magento\Store\Model\Store::DEFAULT_STORE_ID, $storeId]
[Store::DEFAULT_STORE_ID, $storeId],
\Zend_Db::INT_TYPE
);

return $this->connection->fetchAll($select);
Expand Down Expand Up @@ -501,14 +514,14 @@ protected function getTableName($name)
/**
* Get category metadata instance.
*
* @return \Magento\Framework\EntityManager\EntityMetadata
* @return EntityMetadata
*/
private function getCategoryMetadata()
{
if (null === $this->categoryMetadata) {
$metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
$metadataPool = ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\MetadataPool::class);
$this->categoryMetadata = $metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class);
$this->categoryMetadata = $metadataPool->getMetadata(CategoryInterface::class);
}
return $this->categoryMetadata;
}
Expand All @@ -521,8 +534,8 @@ private function getCategoryMetadata()
private function getSkipStaticColumns()
{
if (null === $this->skipStaticColumns) {
$provider = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Indexer\Category\Flat\SkipStaticColumnsProvider::class);
$provider = ObjectManager::getInstance()
->get(SkipStaticColumnsProvider::class);
$this->skipStaticColumns = $provider->get();
}
return $this->skipStaticColumns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ protected function filterIdsByStore(array $ids, $store)
"path = {$rootIdExpr} OR path = {$rootCatIdExpr} OR path like {$catIdExpr}"
)->where(
"entity_id IN (?)",
$ids
$ids,
\Zend_Db::INT_TYPE
);

$resultIds = [];
Expand Down Expand Up @@ -170,27 +171,30 @@ private function buildIndexData(Store $store, $categoriesIdsChunk, $attributesDa
foreach ($categoriesIdsChunk as $categoryId) {
try {
$category = $this->categoryRepository->get($categoryId);
$categoryData = $category->getData();
$linkId = $categoryData[$linkField];

$categoryAttributesData = [];
if (isset($attributesData[$linkId]) && is_array($attributesData[$linkId])) {
$categoryAttributesData = $attributesData[$linkId];
}
$categoryIndexData = $this->buildCategoryIndexData(
$store,
$categoryData,
$categoryAttributesData
);
$data[] = $categoryIndexData;
} catch (NoSuchEntityException $e) {
// ignore
continue;
}

$categoryData = $category->getData();
$linkId = $categoryData[$linkField];

$categoryAttributesData = [];
if (isset($attributesData[$linkId]) && is_array($attributesData[$linkId])) {
$categoryAttributesData = $attributesData[$linkId];
}
$categoryIndexData = $this->buildCategoryIndexData(
$store,
$categoryData,
$categoryAttributesData
);
$data[] = $categoryIndexData;
}
return $data;
}

/**
* Prepare Category data
*
* @param Store $store
* @param array $categoryData
* @param array $categoryAttributesData
Expand All @@ -213,7 +217,8 @@ private function buildCategoryIndexData(Store $store, array $categoryData, array
* Insert or update index data
*
* @param string $tableName
* @param $data
* @param array $data
* @return void
*/
private function updateIndexData($tableName, $data)
{
Expand Down
Loading

0 comments on commit 761b54a

Please sign in to comment.