[5.x]: `getInventoryLevel()` reads every inventory transaction of the location to return one item

未关闭
#4,494 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
mysql, php

调研方向

Start by reading getInventoryLevel() and getInventoryLevelQuery() in the inventory service, focusing on how $inventoryTotals is scoped by location and item. Compare the query plan with derived condition pushdown enabled and disabled, using the reproduction steps and data sizes in the issue. The issue is resolved when reading one item’s level scales with that item’s transactions and stock values remain correct; the suggested index, inner item filter, or batch read are possible approaches.

由索引模型根据 Issue 内容生成。

描述

bug commerce5 Craft Commerce repo:commerce
What happened?
Description

Inventory::getInventoryLevel($item, $location) (and so Purchasable::getStock() via getInventoryLevelsForPurchasable(), once per store location) builds getInventoryLevelQuery(inventoryLocationId: ...) and then filters to the item only in the outer query:

$this->getInventoryLevelQuery(withTrashed: $withTrashed, inventoryLocationId: $inventoryLocationId)
    ->andWhere(['inventoryLocationId' => $inventoryLocationId, 'inventoryItemId' => $inventoryItemId])
    ->one();

The inner query is scoped to the location but not to the item: it cross-joins the location with every inventory item, left-joins all of that location's transactions and groups them. The item condition only reaches it if the database pushes the outer WHERE into the derived table.

Measured in our project on MySQL 8.4 with one location, 329 inventory items and 40,709 transactions (one item has 583 of them), EXPLAIN ANALYZE of the query for one item:

Case What the inner query reads Time
As shipped (MySQL pushes the item condition down itself) index-merge intersection of the inventoryItemId index (583 rows) with the inventoryLocationId index (40,653 rows) ~5.9 ms
Same, with derived_condition_pushdown=off (as on servers without that optimisation) all 40,738 joined rows, grouped into 343 rows, then filtered to 1 ~41 ms
As shipped, plus an index on commerce_inventorytransactions (inventoryItemId, inventoryLocationId) the item's 583 rows only ~1.35 ms

On a clean install (Craft 5.12.4, Commerce 5.7.9, MySQL 8.4.13, default optimizer settings) with one location, 330 inventory-tracked variants and 40,709 transactions (about 123 per item), after ANALYZE TABLE commerce_inventorytransactions:

Case Query for one item getStock() on 301 variants
As shipped (index-merge intersection with the whole location's index range) ~4.2 ms 1.31 s, ~4.3 ms per call
Same, with derived_condition_pushdown=off ~37 ms
As shipped, plus an index on (inventoryItemId, inventoryLocationId) ~0.33 ms 0.11 s, ~0.4 ms per call

The same numbers come out on 5.7.7. In our own project, a loop calling getStock() on 301 variants spent 1.31 s in these reads, about 4.3 ms per call, and the cost grows with the transaction table, which only ever grows. Replacing the per-variant reads with one getInventoryLevelQuery() per location filtered by inventoryItemId IN (...) took the same build from 295 inventory-level queries to 1 (median 3.2 s to 1.2 s, same stock values for every variant).

Actual behavior

Each call costs in proportion to all transactions of the location (or of the location times all items, without derived-condition pushdown).

Related, not the same: #4056 / #4055 (per-request memoization of inventory reads during cart updates).

Suggested change

Any of these, most effective first:

  • Add an index on commerce_inventorytransactions (inventoryItemId, inventoryLocationId); with it MySQL reads only the item's rows (5.9 ms to 1.35 ms above).
  • Let getInventoryLevelQuery() take an optional item id (or ids) and apply it inside $inventoryTotals, as is already done for inventoryLocationId, instead of relying on the server to push the outer condition down.
  • Offer a batch read for many purchasables (one query per location with inventoryItemId IN (...)), for callers like element indexes and exports that read stock for many variants.
Steps to reproduce
  1. Craft 5 + Commerce 5 on MySQL 8.4, one inventory location, a few hundred inventory-tracked variants.
  2. Generate tens of thousands of inventory transactions for that location, spread over the items (for example, many small stock adjustments or orders; inserting rows straight into commerce_inventorytransactions reproduces it too).
  3. Make sure the table statistics are current (ANALYZE TABLE commerce_inventorytransactions;). Straight after a bulk insert the statistics are stale and MySQL may pick the inventoryItemId index alone, which is fast; with current statistics it picks the index-merge plan above.
  4. Call $variant->getStock() (or Commerce::getInstance()->getInventory()->getInventoryLevel($itemId, $locationId)) for each variant, and run EXPLAIN ANALYZE on the SQL it logs.
Expected behavior

Reading one item's level costs roughly in proportion to that item's transactions.

Actual behavior

Each call costs in proportion to all transactions of the location (or of the location times all items, without derived-condition pushdown).

Craft CMS version

5.12.3 (also reproduced on 5.12.4)

Craft Commerce version

5.7.9 (also reproduced on 5.7.7; unchanged from 5.7.6 through 5.x today)

PHP version

8.4

Operating system and version

Linux (Docker)

Database type and version

MySQL 8.4.13

Image driver and version

No response

Installed plugins and versions
主要语言
PHP
星标
242
派生
172
平均合并
21 小时 23 分钟
30 天内合并 PR
76

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

craftcms/commerce 的其他 Issue

查看 craftcms/commerce 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。