Optimize queries: fix N+1, SELECT *, and missing eager loading

N+1 fixes:
- Add withCount('furniture') to rare categories sidebar (prevents N queries)
- Add tags and user.permission eager load in ArticleController
- Add rooms and photos.user eager load in ProfileController
- Add user eager load in MediaApiController (API was returning null user data)

SELECT * fixes:
- Replace WebsitePermission::all()->pluck() with direct pluck()
- Replace WebsiteHousekeepingPermission::all()->pluck() with direct pluck()
- Add select(['id', 'public_name']) to ItemBase query in furniItems()
- Add select(['id', 'name']) to help categories query
- Add select(['id', 'name', 'code']) to languages query

Memory/performance:
- Replace full collection load with aggregate queries in getRareStatistics()
- Add limit(50) to open tickets query in TicketController
This commit is contained in:
root
2026-06-29 18:50:32 +02:00
parent f29ba72591
commit 1acd96b78a
9 changed files with 18 additions and 14 deletions
@@ -16,7 +16,7 @@ readonly class RareValueCategoriesService
public function fetchAllCategories(): Collection
{
return Cache::remember('rare_categories_all', self::CACHE_TTL, fn () => WebsiteRareValueCategory::orderBy('priority')->get());
return Cache::remember('rare_categories_all', self::CACHE_TTL, fn () => WebsiteRareValueCategory::withCount('furniture')->orderBy('priority')->get());
}
public function fetchCategoriesByPriority(): Builder|Collection
@@ -71,13 +71,15 @@ readonly class RareValueCategoriesService
public function getRareStatistics(): array
{
return Cache::remember('rare_statistics', self::CACHE_TTL, function () {
$categories = WebsiteRareValueCategory::withCount('furniture')->get();
$totalCategories = WebsiteRareValueCategory::count();
$totalRares = WebsiteRareValueCategory::sum('furniture_count');
$mostValuable = WebsiteRareValueCategory::orderByDesc('furniture_count')->value('name');
return [
'total_categories' => $categories->count(),
'total_rares' => $categories->sum('furniture_count'),
'most_valuable_category' => $categories->sortByDesc('furniture_count')->first()?->name ?? 'N/A',
'average_rares_per_category' => $categories->avg('furniture_count') ?? 0,
'total_categories' => $totalCategories,
'total_rares' => $totalRares,
'most_valuable_category' => $mostValuable ?? 'N/A',
'average_rares_per_category' => $totalCategories > 0 ? $totalRares / $totalCategories : 0,
];
});
}
@@ -21,7 +21,7 @@ readonly class HousekeepingPermissionsService
$this->permissions = Cache::remember(
self::CACHE_KEY,
now()->addMinutes(self::CACHE_DURATION_MINUTES),
fn (): Collection => WebsiteHousekeepingPermission::all()->pluck('min_rank', 'permission'),
fn (): Collection => WebsiteHousekeepingPermission::pluck('min_rank', 'permission'),
);
}
+1 -1
View File
@@ -18,7 +18,7 @@ readonly class PermissionsService
public function __construct()
{
$data = Cache::remember(self::CACHE_KEY, now()->addMinutes(self::CACHE_DURATION_MINUTES), fn () => WebsitePermission::all()->pluck('min_rank', 'permission')->toArray());
$data = Cache::remember(self::CACHE_KEY, now()->addMinutes(self::CACHE_DURATION_MINUTES), fn () => WebsitePermission::pluck('min_rank', 'permission')->toArray());
$this->permissions = collect($data);
}
+1 -1
View File
@@ -38,7 +38,7 @@ class SettingsService
return collect();
}
return WebsiteLanguage::all();
return WebsiteLanguage::get(['id', 'name', 'code']);
} catch (Throwable) {
return collect();
}