Skip to content

Commit

Permalink
Improve performance of "in" condition on some version of MySQL
Browse files Browse the repository at this point in the history
Fix static test, SVC failures and apply requested changes
  • Loading branch information
ihor-sviziev committed Aug 21, 2020
1 parent 12d3ebf commit 88f10c5
Show file tree
Hide file tree
Showing 42 changed files with 91 additions and 86 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, \Zend_Db::BIGINT_TYPE);
$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, \Zend_Db::BIGINT_TYPE);
$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, \Zend_Db::BIGINT_TYPE);
->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 @@ -425,7 +425,7 @@ private function getLinkIds(array $entityIds)
)->where(
'e.entity_id IN (?)',
$entityIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
);

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

return $this->connection->fetchAll($select);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function filterIdsByStore(array $ids, $store)
)->where(
"entity_id IN (?)",
$ids,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
);

$resultIds = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ protected function createAnchorSelect(Store $store)
$this->connection->quoteInto(
'cc2.parent_id = cc.entity_id AND cc.entity_id NOT IN (?)',
$rootCatIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
),
[]
)->joinInner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private function reindexCategoriesBySelect(Select $basicSelect, $whereCondition,
$this->connection->delete($this->tableMaintainer->getMainTmpTable((int)$store->getId()));
$entityIds = $this->connection->fetchCol($query);
$resultSelect = clone $basicSelect;
$resultSelect->where($whereCondition, $entityIds, \Zend_Db::BIGINT_TYPE);
$resultSelect->where($whereCondition, $entityIds, \Zend_Db::INT_TYPE);
$this->connection->query(
$this->connection->insertFromSelect(
$resultSelect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function getProductIdsWithParents(array $childProductIds): array
->select()
->from(['relation' => $this->getTable('catalog_product_relation')], [])
->distinct(true)
->where('child_id IN (?)', $childProductIds, \Zend_Db::BIGINT_TYPE)
->where('child_id IN (?)', $childProductIds, \Zend_Db::INT_TYPE)
->join(
['cpe' => $this->getTable('catalog_product_entity')],
'relation.parent_id = cpe.' . $fieldForParent,
Expand Down Expand Up @@ -215,7 +215,7 @@ protected function removeEntries()
protected function getNonAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
{
$select = parent::getNonAnchorCategoriesSelect($store);
return $select->where('ccp.product_id IN (?)', $this->limitationByProducts, \Zend_Db::BIGINT_TYPE);
return $select->where('ccp.product_id IN (?)', $this->limitationByProducts, \Zend_Db::INT_TYPE);
}

/**
Expand All @@ -227,7 +227,7 @@ protected function getNonAnchorCategoriesSelect(\Magento\Store\Model\Store $stor
protected function getAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
{
$select = parent::getAnchorCategoriesSelect($store);
return $select->where('ccp.product_id IN (?)', $this->limitationByProducts, \Zend_Db::BIGINT_TYPE);
return $select->where('ccp.product_id IN (?)', $this->limitationByProducts, \Zend_Db::INT_TYPE);
}

/**
Expand All @@ -239,7 +239,7 @@ protected function getAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
protected function getAllProducts(\Magento\Store\Model\Store $store)
{
$select = parent::getAllProducts($store);
return $select->where('cp.entity_id IN (?)', $this->limitationByProducts, \Zend_Db::BIGINT_TYPE);
return $select->where('cp.entity_id IN (?)', $this->limitationByProducts, \Zend_Db::INT_TYPE);
}

/**
Expand All @@ -265,7 +265,7 @@ private function getCategoryIdsFromIndex(array $productIds): array
$storeCategories = $this->connection->fetchCol(
$this->connection->select()
->from($this->getIndexTable($store->getId()), ['category_id'])
->where('product_id IN (?)', $productIds, \Zend_Db::BIGINT_TYPE)
->where('product_id IN (?)', $productIds, \Zend_Db::INT_TYPE)
->distinct()
);
$categoryIds[] = $storeCategories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ protected function _updateTemporaryTableByStoreValues(
);
if (!empty($changedIds)) {
$select->where(
$this->_connection->quoteInto('et.entity_id IN (?)', $changedIds, \Zend_Db::BIGINT_TYPE)
$this->_connection->quoteInto('et.entity_id IN (?)', $changedIds, \Zend_Db::INT_TYPE)
);
}
$sql = $select->crossUpdateFromSelect(['et' => $temporaryFlatTableName]);
Expand Down Expand Up @@ -382,7 +382,7 @@ protected function _updateTemporaryTableByStoreValues(
$this->_connection->quoteInto(
'et.entity_id IN (?)',
$changedIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ protected function _fillTemporaryTable(

if (!empty($changedIds)) {
$select->where(
$this->_connection->quoteInto('e.entity_id IN (?)', $changedIds, \Zend_Db::BIGINT_TYPE)
$this->_connection->quoteInto('e.entity_id IN (?)', $changedIds, \Zend_Db::INT_TYPE)
);
}

Expand All @@ -358,7 +358,7 @@ protected function _fillTemporaryTable(
if (count($valueColumns) > 1) {
if (!empty($changedIds)) {
$selectValue->where(
$this->_connection->quoteInto('e.entity_id IN (?)', $changedIds, \Zend_Db::BIGINT_TYPE)
$this->_connection->quoteInto('e.entity_id IN (?)', $changedIds, \Zend_Db::INT_TYPE)
);
}
$sql = $selectValue->insertFromSelect($temporaryValueTableName, $valueColumns, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private function deleteIndexData(array $entityIds)
$select = $this->getConnection()->select()->from(
['index_price' => $this->tableMaintainer->getMainTableByDimensions($dimensions)],
null
)->where('index_price.entity_id IN (?)', $entityIds, \Zend_Db::BIGINT_TYPE);
)->where('index_price.entity_id IN (?)', $entityIds, \Zend_Db::INT_TYPE);
$query = $select->deleteFromSelect('index_price');
$this->getConnection()->query($query);
}
Expand Down Expand Up @@ -547,7 +547,7 @@ private function getProductsTypes(array $changedIds = [])
['entity_id', 'type_id']
);
if ($changedIds) {
$select->where('entity_id IN (?)', $changedIds, \Zend_Db::BIGINT_TYPE);
$select->where('entity_id IN (?)', $changedIds, \Zend_Db::INT_TYPE);
}
$pairs = $this->getConnection()->fetchPairs($select);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function findAttributeSetIdsByProductIds(array $productIds)
->getSelect()
->reset(Select::COLUMNS)
->columns(ProductInterface::ATTRIBUTE_SET_ID)
->where('entity_id IN (?)', $productIds, \Zend_Db::BIGINT_TYPE)
->where('entity_id IN (?)', $productIds, \Zend_Db::INT_TYPE)
->group(ProductInterface::ATTRIBUTE_SET_ID);
$result = $collection->getConnection()->fetchCol($select);
return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function get(array $skus)
->select()
->from($this->attributeResource->getTable($this->table));
return $this->attributeResource->getConnection()->fetchAll(
$select->where($this->getEntityLinkField() . ' IN (?)', $ids, \Zend_Db::BIGINT_TYPE)
$select->where($this->getEntityLinkField() . ' IN (?)', $ids, \Zend_Db::INT_TYPE)
->where('attribute_id = ?', $this->getAttributeId())
);
}
Expand Down Expand Up @@ -214,13 +214,13 @@ private function getAttributeId()
*/
private function retrieveAffectedIds(array $skus)
{
$affectedIds = [[]];
$affectedIds = [];

foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $productIds) {
$affectedIds[] = array_keys($productIds);
}

return array_unique(array_merge(...$affectedIds));
return array_unique(array_merge([], ...$affectedIds));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function get(array $ids)
{
$select = $this->tierpriceResource->getConnection()->select()->from($this->tierpriceResource->getMainTable());
return $this->tierpriceResource->getConnection()->fetchAll(
$select->where($this->getEntityLinkField() . ' IN (?)', $ids, \Zend_Db::BIGINT_TYPE)
$select->where($this->getEntityLinkField() . ' IN (?)', $ids, \Zend_Db::INT_TYPE)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function _getLoadAttributesSelect($object, $table)
->select()
->from(['attr_table' => $table], [])
->where("attr_table.{$this->getLinkField()} = ?", $object->getData($this->getLinkField()))
->where('attr_table.store_id IN (?)', $storeIds, \Zend_Db::BIGINT_TYPE);
->where('attr_table.store_id IN (?)', $storeIds, \Zend_Db::INT_TYPE);

if ($setId) {
$select->join(
Expand Down Expand Up @@ -565,7 +565,7 @@ public function getAttributeRawValue($entityId, $attribute, $store)
$connection->quoteInto(
'default_value.attribute_id IN (?)',
array_keys($_attributes),
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
),
"default_value.{$this->getLinkField()} = e.{$this->getLinkField()}",
'default_value.store_id = 0',
Expand Down Expand Up @@ -596,7 +596,7 @@ public function getAttributeRawValue($entityId, $attribute, $store)
$connection->quoteInto(
'store_value.attribute_id IN (?)',
array_keys($_attributes),
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
),
"store_value.{$this->getLinkField()} = e.{$this->getLinkField()}",
'store_value.store_id = :store_id',
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValu
)->where(
'ce.entity_id IN (?)',
$entityIdsFilter,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
);
$this->entitiesWhereAttributesIs[$entityIdsFilterHash][$attribute->getId()][$expectedValue] =
$this->getConnection()->fetchCol($selectEntities, $bind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected function _loadNodes($parentNode = null, $recursionLevel = 0, $storeId
$inactiveCategories = $this->getInactiveCategoryIds();

if (!empty($inactiveCategories)) {
$select->where('main_table.entity_id NOT IN (?)', $inactiveCategories, \Zend_Db::BIGINT_TYPE);
$select->where('main_table.entity_id NOT IN (?)', $inactiveCategories, \Zend_Db::INT_TYPE);
}

// Allow extensions to modify select (e.g. add custom category attributes to select)
Expand Down Expand Up @@ -682,7 +682,7 @@ public function getAnchorsAbove(array $filterIds, $storeId = 0)
)->where(
'entity_id IN (?)',
$filterIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
);

return $this->getConnection()->fetchCol($select);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ protected function _getLoadAttributesSelect($table, $attributeIds = [])
)->where(
"e.entity_id IN (?)",
array_keys($this->_itemsById),
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)->where(
't_d.attribute_id IN (?)',
$attributeIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)->joinLeft(
['t_s' => $table],
implode(' AND ', $joinCondition),
Expand All @@ -195,11 +195,11 @@ protected function _getLoadAttributesSelect($table, $attributeIds = [])
)->where(
"e.entity_id IN (?)",
array_keys($this->_itemsById),
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)->where(
'attribute_id IN (?)',
$attributeIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)->where(
'store_id = ?',
$this->getDefaultStoreId()
Expand Down
7 changes: 4 additions & 3 deletions app/code/Magento/Catalog/Model/ResourceModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function getWebsiteIdsByProductIds($productIds)
)->where(
'product_id IN (?)',
$productIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
);
$productsWebsites = [];
foreach ($this->getConnection()->fetchAll($select) as $productInfo) {
Expand Down Expand Up @@ -361,7 +361,7 @@ private function deleteSelectedEntityAttributeRows(DataObject $product, array $a
$entityId = $product->getData($entityIdField);
foreach ($backendTables as $backendTable => $attributes) {
$connection = $this->getConnection();
$where = $connection->quoteInto('attribute_id IN (?)', $attributes, \Zend_Db::BIGINT_TYPE);
$where = $connection->quoteInto('attribute_id IN (?)', $attributes, \Zend_Db::INT_TYPE);
$where .= $connection->quoteInto(" AND {$entityIdField} = ?", $entityId);
$connection->delete($backendTable, $where);
}
Expand Down Expand Up @@ -454,6 +454,7 @@ public function getAvailableInCategories($object)
// fetching all parent IDs, including those are higher on the tree
$entityId = (int)$object->getEntityId();
if (!isset($this->availableCategoryIdsCache[$entityId])) {
$unionTables = [];
foreach ($this->_storeManager->getStores() as $store) {
$unionTables[] = $this->getAvailableInCategoriesSelect(
$entityId,
Expand Down Expand Up @@ -599,7 +600,7 @@ public function getProductsSku(array $productIds)
)->where(
'entity_id IN (?)',
$productIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
);
return $this->getConnection()->fetchAll($select);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ protected function doAddWebsiteNamesToResult()
)->where(
'product_website.product_id IN (?)',
array_keys($productWebsites),
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)->where(
'website.website_id > ?',
0
Expand Down Expand Up @@ -1359,15 +1359,15 @@ public function addCountToCategories($categoryCollection)
$anchorStmt = clone $select;
$anchorStmt->limit();
//reset limits
$anchorStmt->where('count_table.category_id IN (?)', $isAnchor, \Zend_Db::BIGINT_TYPE);
$anchorStmt->where('count_table.category_id IN (?)', $isAnchor, \Zend_Db::INT_TYPE);
$productCounts += $this->getConnection()->fetchPairs($anchorStmt);
$anchorStmt = null;
}
if ($isNotAnchor) {
$notAnchorStmt = clone $select;
$notAnchorStmt->limit();
//reset limits
$notAnchorStmt->where('count_table.category_id IN (?)', $isNotAnchor, \Zend_Db::BIGINT_TYPE);
$notAnchorStmt->where('count_table.category_id IN (?)', $isNotAnchor, \Zend_Db::INT_TYPE);
$notAnchorStmt->where('count_table.is_parent = 1');
$productCounts += $this->getConnection()->fetchPairs($notAnchorStmt);
$notAnchorStmt = null;
Expand Down Expand Up @@ -2166,7 +2166,7 @@ public function addCategoryIds()
$select = $this->getConnection()->select();

$select->from($this->_productCategoryTable, ['product_id', 'category_id']);
$select->where('product_id IN (?)', $ids, \Zend_Db::BIGINT_TYPE);
$select->where('product_id IN (?)', $ids, \Zend_Db::INT_TYPE);

$data = $this->getConnection()->fetchAll($select);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public function getProductImages($product, $storeIds)
)->where(
'store_id IN (?)',
$storeIds,
\Zend_Db::BIGINT_TYPE
\Zend_Db::INT_TYPE
)->where(
'attribute_code IN (?)',
['small_image', 'thumbnail', 'image']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ protected function _movePriceDataToIndexTable($entityIds = null)
$select = $connection->select()->from($table, $columns);

if ($entityIds !== null) {
$select->where('entity_id in (?)', count($entityIds) > 0 ? $entityIds : 0, \Zend_Db::BIGINT_TYPE);
$select->where('entity_id in (?)', count($entityIds) > 0 ? $entityIds : 0, \Zend_Db::INT_TYPE);
}

$query = $select->insertFromSelect($this->getIdxTable(), [], false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private function getTierPriceSelect(bool $isAllWebsites, bool $isAllCustomerGrou
[]
);
if (!empty($entityIds)) {
$select->where('entity.entity_id IN (?)', $entityIds, \Zend_Db::BIGINT_TYPE);
$select->where('entity.entity_id IN (?)', $entityIds, \Zend_Db::INT_TYPE);
}
$this->joinWebsites($select, $isAllWebsites);
$this->joinCustomerGroups($select, $isAllCustomerGroups);
Expand Down
Loading

0 comments on commit 88f10c5

Please sign in to comment.