Skip to content

Commit 0a390bb

Browse files
authored
Merge pull request #4788 from magento-tango/MC-19791
[tango] MC-19791: Poor performance on sales order update - string to integer
2 parents d9d4b82 + 2c800de commit 0a390bb

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/GroupTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testSaveWithReservedId()
127127
]
128128
)
129129
->getMockForAbstractClass();
130-
$dbAdapter->expects($this->any())->method('describeTable')->willReturn([]);
130+
$dbAdapter->expects($this->any())->method('describeTable')->willReturn(['customer_group_id' => []]);
131131
$dbAdapter->expects($this->any())->method('update')->willReturnSelf();
132132
$dbAdapter->expects($this->once())->method('lastInsertId')->willReturn($expectedId);
133133
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)

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

+30-6
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,24 @@ protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
784784
}
785785
}
786786

787+
/**
788+
* Check if column data type is numeric
789+
*
790+
* Based on column description
791+
*
792+
* @param array $columnDescription
793+
* @return bool
794+
*/
795+
private function isNumericValue(array $columnDescription): bool
796+
{
797+
$result = true;
798+
if (!empty($columnDescription['DATA_TYPE'])
799+
&& in_array($columnDescription['DATA_TYPE'], ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'])) {
800+
$result = false;
801+
}
802+
return $result;
803+
}
804+
787805
/**
788806
* Update existing object
789807
*
@@ -793,29 +811,35 @@ protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
793811
*/
794812
protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
795813
{
796-
$condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
814+
$connection = $this->getConnection();
815+
$tableDescription = $connection->describeTable($this->getMainTable());
816+
$preparedValue = $connection->prepareColumnValue($tableDescription[$this->getIdFieldName()], $object->getId());
817+
$condition = (!$this->isNumericValue($tableDescription[$this->getIdFieldName()]))
818+
? sprintf('%s=%d', $this->getIdFieldName(), $preparedValue)
819+
: $connection->quoteInto($this->getIdFieldName() . '=?', $preparedValue);
820+
797821
/**
798822
* Not auto increment primary key support
799823
*/
800824
if ($this->_isPkAutoIncrement) {
801825
$data = $this->prepareDataForUpdate($object);
802826
if (!empty($data)) {
803-
$this->getConnection()->update($this->getMainTable(), $data, $condition);
827+
$connection->update($this->getMainTable(), $data, $condition);
804828
}
805829
} else {
806-
$select = $this->getConnection()->select()->from(
830+
$select = $connection->select()->from(
807831
$this->getMainTable(),
808832
[$this->getIdFieldName()]
809833
)->where(
810834
$condition
811835
);
812-
if ($this->getConnection()->fetchOne($select) !== false) {
836+
if ($connection->fetchOne($select) !== false) {
813837
$data = $this->prepareDataForUpdate($object);
814838
if (!empty($data)) {
815-
$this->getConnection()->update($this->getMainTable(), $data, $condition);
839+
$connection->update($this->getMainTable(), $data, $condition);
816840
}
817841
} else {
818-
$this->getConnection()->insert(
842+
$connection->insert(
819843
$this->getMainTable(),
820844
$this->_prepareDataForSave($object)
821845
);

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,15 @@ 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
);
431432
$registryMock = $this->createMock(\Magento\Framework\Registry::class);
432-
$resourceMock = $this->createPartialMock(AbstractDb::class, [
433-
'_construct',
434-
'getConnection',
435-
'__wakeup',
436-
'getIdFieldName'
437-
]);
433+
$resourceMock = $this->createPartialMock(
434+
AbstractDb::class,
435+
['_construct', 'getConnection', '__wakeup', 'getIdFieldName']
436+
);
438437
$connectionInterfaceMock = $this->createMock(AdapterInterface::class);
439438
$resourceMock->expects($this->any())
440439
->method('getConnection')
@@ -453,6 +452,7 @@ public function testPrepareDataForUpdate()
453452
$this->_resourcesMock->expects($this->any())->method('getTableName')->with($data)->will(
454453
$this->returnValue('tableName')
455454
);
455+
456456
$mainTableReflection = new \ReflectionProperty(
457457
AbstractDb::class,
458458
'_mainTable'
@@ -467,6 +467,13 @@ public function testPrepareDataForUpdate()
467467
$idFieldNameReflection->setValue($this->_model, 'idFieldName');
468468
$connectionMock->expects($this->any())->method('save')->with('tableName', 'idFieldName');
469469
$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);
470477
$abstractModelMock->setIdFieldName('id');
471478
$abstractModelMock->setData(
472479
[

0 commit comments

Comments
 (0)