Skip to content

Commit 29f99e6

Browse files
author
Oleksandr Iegorov
committed
MC-19791: Poor performance on sales order update - string to integer
1 parent 650a4a1 commit 29f99e6

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Model\ResourceModel\AbstractResource;
1212
use Magento\Framework\DB\Adapter\DuplicateException;
1313
use Magento\Framework\Phrase;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
1415

1516
/**
1617
* Abstract resource model
@@ -301,7 +302,7 @@ public function getTable($tableName)
301302
* Get connection by resource name
302303
*
303304
* @param string $resourceName
304-
* @return \Magento\Framework\DB\Adapter\AdapterInterface|false
305+
* @return AdapterInterface|false
305306
*/
306307
protected function _getConnection($resourceName)
307308
{
@@ -320,7 +321,7 @@ protected function _getConnection($resourceName)
320321
/**
321322
* Get connection
322323
*
323-
* @return \Magento\Framework\DB\Adapter\AdapterInterface|false
324+
* @return AdapterInterface|false
324325
*/
325326
public function getConnection()
326327
{
@@ -793,13 +794,10 @@ protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
793794
*/
794795
protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
795796
{
796-
$tableDescription = $this->getConnection()
797-
->describeTable($this->getMainTable());
798-
$preparedValue = $this->getConnection()
799-
->prepareColumnValue(
800-
$tableDescription[$this->getIdFieldName()],
801-
$object->getId()
802-
);
797+
/** @var AdapterInterface $connection */
798+
$connection = $this->getConnection();
799+
$tableDescription = $connection->describeTable($this->getMainTable());
800+
$preparedValue = $connection->prepareColumnValue($tableDescription[$this->getIdFieldName()], $object->getId());
803801
$condition = $this->getIdFieldName() . '=' . $preparedValue;
804802

805803
/**
@@ -808,22 +806,22 @@ protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
808806
if ($this->_isPkAutoIncrement) {
809807
$data = $this->prepareDataForUpdate($object);
810808
if (!empty($data)) {
811-
$this->getConnection()->update($this->getMainTable(), $data, $condition);
809+
$connection->update($this->getMainTable(), $data, $condition);
812810
}
813811
} else {
814-
$select = $this->getConnection()->select()->from(
812+
$select = $connection->select()->from(
815813
$this->getMainTable(),
816814
[$this->getIdFieldName()]
817815
)->where(
818816
$condition
819817
);
820-
if ($this->getConnection()->fetchOne($select) !== false) {
818+
if ($connection->fetchOne($select) !== false) {
821819
$data = $this->prepareDataForUpdate($object);
822820
if (!empty($data)) {
823-
$this->getConnection()->update($this->getMainTable(), $data, $condition);
821+
$connection->update($this->getMainTable(), $data, $condition);
824822
}
825823
} else {
826-
$this->getConnection()->insert(
824+
$connection->insert(
827825
$this->getMainTable(),
828826
$this->_prepareDataForSave($object)
829827
);

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/Db/AbstractDbTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ public function testPrepareDataForUpdate()
425425
$connectionMock = $this->getMockBuilder(AdapterInterface::class)
426426
->setMethods(['save'])
427427
->getMockForAbstractClass();
428+
428429
$context = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
429430
\Magento\Framework\Model\Context::class
430431
);
@@ -451,6 +452,7 @@ public function testPrepareDataForUpdate()
451452
$this->_resourcesMock->expects($this->any())->method('getTableName')->with($data)->will(
452453
$this->returnValue('tableName')
453454
);
455+
454456
$mainTableReflection = new \ReflectionProperty(
455457
AbstractDb::class,
456458
'_mainTable'
@@ -465,6 +467,13 @@ public function testPrepareDataForUpdate()
465467
$idFieldNameReflection->setValue($this->_model, 'idFieldName');
466468
$connectionMock->expects($this->any())->method('save')->with('tableName', 'idFieldName');
467469
$connectionMock->expects($this->any())->method('quoteInto')->will($this->returnValue('idFieldName'));
470+
$connectionMock->expects($this->any())
471+
->method('describeTable')
472+
->with('tableName')
473+
->willReturn(['idFieldName' => []]);
474+
$connectionMock->expects($this->any())
475+
->method('prepareColumnValue')
476+
->willReturn(0);
468477
$abstractModelMock->setIdFieldName('id');
469478
$abstractModelMock->setData(
470479
[

0 commit comments

Comments
 (0)