Skip to content

A free open source IT asset/license management system

Small consumables index query optimization

snipe57cd75221991
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index 185a4ffcb0..92adfa15bc 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -46,8 +46,15 @@ class ConsumablesController extends Controller
// consumables_users_count feeds the stockroom-remaining math (qty minus total ever
// distributed, unchanged by mark-consumed). outstanding_assignments_count feeds
// isDeletable() and the archive guard so neither fires a per-row count query.
+ //
+ // Both counts walk the consumableAssignments hasMany directly (which wraps
+ // consumables_users) rather than the unconstrainedUsers belongsToMany, so the
+ // subquery is a single-table COUNT against consumables_users.consumable_id
+ // instead of INNER JOIN users. The users JOIN was dead weight here anyway
+ // (we only want a count of pivot rows), and dropping it is a measurable win
+ // on hundreds-of-thousands-of-consumables fleets.
$consumables = Consumable::with('company', 'location', 'category', 'defaultSupplier', 'manufacturer', 'orderItems.order.supplier')
- ->withCount('unconstrainedUsers as consumables_users_count')
+ ->withCount('consumableAssignments as consumables_users_count')
->withCount('outstandingAssignments as outstanding_assignments_count');
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
@@ -616,7 +623,7 @@ class ConsumablesController extends Controller
public function requestable(Request $request): array
{
$query = Consumable::with('category', 'location', 'company', 'manufacturer', 'requests')
- ->withCount('unconstrainedUsers as consumables_users_count')
+ ->withCount('consumableAssignments as consumables_users_count')
->withCount('outstandingAssignments as outstanding_assignments_count')
->Requestable();
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 299cde8d51..c65ed7162f 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -392,6 +392,12 @@ class Consumable extends SnipeModel
/**
* Same as users() but without FMCS scoping applied so counts
* reflect every unit checked out regardless of the viewer's company.
+ *
+ * For withCount in the list controllers prefer consumableAssignments:
+ * this relation INNER JOINs users for a count that never reads any
+ * user field, which is dead weight on fleets with large
+ * consumables_users pivots. Kept intact for any caller that needs
+ * the actual User objects (e.g. iterating recipients).
*/
public function unconstrainedUsers(): BelongsToMany
{
@@ -461,7 +467,10 @@ class Consumable extends SnipeModel
*/
public function numCheckedOut()
{
- return $this->consumables_users_count ?? $this->unconstrainedUsers()->count();
+ // Fallback walks the consumableAssignments hasMany so this
+ // path matches the shape the list controllers use via withCount
+ // (single-table COUNT on consumables_users, no users JOIN).
+ return $this->consumables_users_count ?? $this->consumableAssignments()->count();
}
/**