Skip to content

Commit

Permalink
Fix memory-hog query in AssetCountForSidebar middleware
Browse files Browse the repository at this point in the history
https://github.com/snipe/snipe-it/pull/14702/files introduced a bug
where instead of doing a quick `select count(*)` of assets, it did a `select *` of
assets, moving the count from the database to the PHP process.

This caused OOM issues in memory-constrained environments with lots of
assets, and also presented a speed issue even when memory limited were
increased.

Additionally, given this populates the sidebar, this was likely an issue
on every page load that included the sidebar.

The fix is simply removing the `all()->`, ending up with Asset::count(),
which yields the desired `select count(*)` DB query.
  • Loading branch information
Jeremy Price committed May 10, 2024
1 parent 46779ca commit 2adc4ff
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion app/Http/Middleware/AssetCountForSidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle($request, Closure $next)
}

try {
$total_assets = Asset::all()->count();
$total_assets = Asset::count();
if ($settings->show_archived_in_list != '1') {
$total_assets -= Asset::Archived()->count();
}
Expand Down

0 comments on commit 2adc4ff

Please sign in to comment.