commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[5.x]: Inventory::getInventoryFulfillmentLevels() causing slowdowns on cart updates.

Open joshuapease opened this issue 8 months ago • 3 comments

Description

We've noticed some really big slowdowns around inventory and cart updates.

LineItem::getFulfilledTotalQuantity() which subsequently calls Inventory::getInventoryFulfillmentLevels() calls the same query to commerce_inventorytransactions for each line item.

This project has 600,000+ skus, which makes this query especially slow.

We also don't track inventory on this project, so it seems like their might be some efficiency gains if the query could just be skipped for purchasables that don't have $inventoryTracked = true

Image

Image

I noticed this PR was opened recently too

  • https://github.com/craftcms/commerce/pull/4055/files

Steps to reproduce

  1. Have a high number of Variants in Craft (600,000+)
  2. Add multiple items to cart

Expected behavior

  • Only one call to Inventory::getInventoryFulfillmentLevels()
  • Ideally this query is bypassed when a purchasable doesn't track inventory.

Actual behavior

  • Calls to Inventory::getInventoryFulfillmentLevels() grow as line items increase

Craft CMS version

5.7.4

Craft Commerce version

5.3.13

PHP version

8.3

Operating system and version

DDEV

Database type and version

8.0.40

Image driver and version

No response

Installed plugins and versions

joshuapease avatar Jun 21 '25 22:06 joshuapease

Thanks for reporting this. It seems this is only affecting carts loaded over Ajax (in your case Sprig).

I have made a fix that should help speed things up for you (and I am working on some more).

To get the fix early, Make sure you have already upgraded to Commerce 5.4.0 and then change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce": "5.x-dev#237478671bddf02b71066cb65f06d66511e0a13e as 5.4.0",
  "...": "..."
}

Then run composer update.

We will update this ticket once the release is out.

lukeholder avatar Jun 23 '25 09:06 lukeholder

Thanks @lukeholder that made an improvement

joshuapease avatar Jun 23 '25 17:06 joshuapease

We found that memoizing the inventory levels, transactions and fulfillments significantly improved things with 500k+ inventory items.

The cause of problem seemed to be that those queries executed quite a few times for each cart operation. It was significantly more when we would add multiple items to the cart at once.

johnnynotsolucky avatar Jul 18 '25 11:07 johnnynotsolucky

I have made another small fix that should help speed things up for you.

To get the fix early, Make sure you have already upgraded to Commerce 5.4.3 and then change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce": "5.x-dev#3a6460ba47afe1fd14b04f2302df9ff1ce7479b5 as 5.4.3",
  "...": "..."
}

Then run composer update.

We will update this ticket once the release is out. Thanks!

lukeholder avatar Jul 30 '25 10:07 lukeholder

Commerce 5.4.4 is out with that latest change.

brandonkelly avatar Jul 31 '25 18:07 brandonkelly