diff --git a/vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock.php b/vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock.php
index eaea6ee560c..0a674c01365 100644
--- a/vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock.php
+++ b/vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock.php
@@ -125,10 +125,19 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
return [];
}
$itemTable = $this->getTable('cataloginventory_stock_item');
- $select = $this->getConnection()->select()->from(['si' => $itemTable])
+
+ //get indexed entries for row level lock instead of table lock
+ $itemIds = [];
+ $preSelect = $this->getConnection()->select()->from($itemTable, 'item_id')
->where('website_id = ?', $websiteId)
- ->where('product_id IN(?)', $productIds)
- ->forUpdate(true);
+ ->where('product_id IN(?)', $productIds);
+ foreach ($this->getConnection()->query($preSelect)->fetchAll() as $item) {
+ $itemIds[] = $item['item_id'];
+ }
+
+ $select = $this->getConnection()->select()->from(['si' => $itemTable])
+ ->where('item_id IN (?)', $itemIds);
+ $selectString = (string)$select . ' LOCK IN SHARE MODE';
$productTable = $this->getTable('catalog_product_entity');
$selectProducts = $this->getConnection()->select()->from(['p' => $productTable], [])
@@ -141,18 +150,17 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
);
$items = [];
- foreach ($this->getConnection()->query($select)->fetchAll() as $si) {
+ foreach ($this->getConnection()->query($selectString)->fetchAll() as $si) {
$items[$si['product_id']] = $si;
}
foreach ($this->getConnection()->fetchAll($selectProducts) as $p) {
$items[$p['product_id']]['type_id'] = $p['type_id'];
}
-
return $items;
}
/**
- * {@inheritdoc}
+ * @inheritdoc
*/
public function correctItemsQty(array $items, $websiteId, $operator)
{
@@ -161,18 +169,13 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
}
$connection = $this->getConnection();
- $conditions = [];
+ $itemTable = $this->getTable('cataloginventory_stock_item');
+ $connection->beginTransaction();
foreach ($items as $productId => $qty) {
- $case = $connection->quoteInto('?', $productId);
- $result = $connection->quoteInto("qty{$operator}?", $qty);
- $conditions[$case] = $result;
+ $value = new \Zend_Db_Expr($connection->quoteInto("qty{$operator}?", $qty));
+ $where = ['website_id = ?' => $websiteId, 'product_id = ?' => $productId];
+ $connection->update($itemTable, ['qty' => $value], $where);
}
-
- $value = $connection->getCaseSql('product_id', $conditions, 'qty');
- $where = ['product_id IN (?)' => array_keys($items), 'website_id = ?' => $websiteId];
-
- $connection->beginTransaction();
- $connection->update($this->getTable('cataloginventory_stock_item'), ['qty' => $value], $where);
$connection->commit();
}
diff --git a/vendor/magento/module-catalog-inventory/etc/db_schema.xml b/vendor/magento/module-catalog-inventory/etc/db_schema.xml
index 67a200eb371..21ef3aa4c46 100644
--- a/vendor/magento/module-catalog-inventory/etc/db_schema.xml
+++ b/vendor/magento/module-catalog-inventory/etc/db_schema.xml
@@ -87,6 +87,10 @@