From 2adc4ffa963134f4e17ce204f9e231f321bc201e Mon Sep 17 00:00:00 2001 From: Jeremy Price Date: Fri, 10 May 2024 12:54:40 -0700 Subject: [PATCH] Fix memory-hog query in AssetCountForSidebar middleware 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. --- app/Http/Middleware/AssetCountForSidebar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Middleware/AssetCountForSidebar.php b/app/Http/Middleware/AssetCountForSidebar.php index 9d81eca93f70..fa439f7753ac 100644 --- a/app/Http/Middleware/AssetCountForSidebar.php +++ b/app/Http/Middleware/AssetCountForSidebar.php @@ -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(); }