Skip to content

Improve the performance of the SQL query generated for updating stock…#3457

Open
igorwulff wants to merge 1 commit into
magento:developfrom
igorwulff:patch-1
Open

Improve the performance of the SQL query generated for updating stock…#3457
igorwulff wants to merge 1 commit into
magento:developfrom
igorwulff:patch-1

Conversation

@igorwulff

Copy link
Copy Markdown

… status after an order is placed

The input parameter $dataForUpdate uses SKU as the array key. Since SKUs can be numeric, PHP will automatically cast such keys to integers when used in an array. Reference: https://www.php.net/manual/en/language.types.array.php "Specifically, strings that represent valid decimal integers will be converted to integers"

As a result, the generated SQL query may contain sku=3938599 instead of sku='3938599'. This leads MySQL to perform implicit type conversion, causing a full table scan where every row is loaded into memory and compared by converting string values to numbers one by one. Reference: https://dev.mysql.com/doc/refman/8.0/en/type-conversion.html

We cannot fix the issue at the point where $dataForUpdate is generated (\Magento\InventoryIndexer\Model\Queue\GetSalabilityDataForUpdate::execute), since this logic is reused elsewhere and changing the data type could introduce side effects. However, in this specific function—where the data is used to build the SQL query—we can explicitly cast the SKU to a string. This prevents MySQL from performing a full table scan and ensures proper query behavior.

… status after an order is placed

The input parameter `$dataForUpdate` uses SKU as the array key. Since SKUs can be numeric,
PHP will automatically cast such keys to integers when used in an array.
Reference: https://www.php.net/manual/en/language.types.array.php
"Specifically, strings that represent valid decimal integers will be converted to integers"

As a result, the generated SQL query may contain `sku=3938599` instead of `sku='3938599'`.
This leads MySQL to perform implicit type conversion, causing a full table scan where every row is loaded into memory and compared by converting string values to numbers one by one.
Reference: https://dev.mysql.com/doc/refman/8.0/en/type-conversion.html

We cannot fix the issue at the point where `$dataForUpdate` is generated (`\Magento\InventoryIndexer\Model\Queue\GetSalabilityDataForUpdate::execute`),
since this logic is reused elsewhere and changing the data type could introduce side effects.
However, in this specific function—where the data is used to build the SQL query—we can explicitly cast the SKU to a string.
This prevents MySQL from performing a full table scan and ensures proper query behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

1 participant