[5.x]: `getInventoryLevel()` reads every inventory transaction of the location to return one item
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 55/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- mysql, php
- 領域
- databases, performance
調査の方向性
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 の本文から書いたものです。
説明
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 forinventoryLocationId, 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
- Craft 5 + Commerce 5 on MySQL 8.4, one inventory location, a few hundred inventory-tracked variants.
- 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_inventorytransactionsreproduces it too). - Make sure the table statistics are current (
ANALYZE TABLE commerce_inventorytransactions;). Straight after a bulk insert the statistics are stale and MySQL may pick theinventoryItemIdindex alone, which is fast; with current statistics it picks the index-merge plan above. - Call
$variant->getStock()(orCommerce::getInstance()->getInventory()->getInventoryLevel($itemId, $locationId)) for each variant, and runEXPLAIN ANALYZEon 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分
- マージ済み PR(30日)
- 76
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
craftcms/commerce のほかの issue
-
[6.x]: Commerce can't boot without `craftcms/yii2-adapter`再び着手できるかも @lukeholder が 30 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンrepo:commerce
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
craftcms/commerce#4402 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
[5.x]: Catalog pricing queue lock is held until the product save commits, so parallel saves run one at a time対応中かも @nfourtythree が 30 日前に担当しました。 オープンrepo:commerce
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
craftcms/commerce#4389 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
bug commerce5 Craft Commerce
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
craftcms/commerce#4264 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
💡 enhancement commerce5 Craft Commerce
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
craftcms/commerce#4156 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[4.x]: Multiple empty carts when refreshing fast with autoSetNewCartAddresses setting再び着手できるかも @lukeholder が 596 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンbug commerce4 Craft Commerce
craftcms/commerce#3973 · コメント 6 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
craftcms/commerce の issue をすべて見る
似ている issue
-
Document lexicon.default.userconfig and lexicon.default.userconfig.enforced in config.sample.phpオープンenhancement feature: settings good first issue
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
nextcloud/integration_openai#480 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
a11y
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
omeka/plugin-Contribution#84 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
recca0120/vscode-phpunit#620 ·
メンテナーはふだん 1 日以内に返信