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
@@ -17,6 +17,7 @@ class MediaApiController extends Controller
$photos = CameraWeb::query()
->where('visible', true)
->latest('id')
->with('user:id,username,look')
->paginate(12);
return response()->json([
@@ -29,7 +29,7 @@ class ArticleController extends Controller
public function show(WebsiteArticle $article): View
{
$article->load(['user:id,username,look', 'comments.user:id,username,look']);
$article->load(['user:id,username,look', 'user.permission:id,rank_name', 'comments.user:id,username,look', 'tags']);
$reactions = $article->reactions()
->with('user:id,username')
@@ -29,14 +29,14 @@ class TicketController extends Controller
private function getCachedCategories(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('help_categories', 3600, fn () => WebsiteHelpCenterCategory::get());
return Cache::remember('help_categories', 3600, fn () => WebsiteHelpCenterCategory::get(['id', 'name']));
}
public function create(): View
{
return view('help-center.tickets.create', [
'categories' => $this->getCachedCategories(),
'openTickets' => WebsiteHelpCenterTicket::where('open', true)->where('user_id', Auth::id())->get(),
'openTickets' => WebsiteHelpCenterTicket::where('open', true)->where('user_id', Auth::id())->limit(50)->get(),
]);
}
@@ -16,7 +16,8 @@ class ProfileController extends Controller
'friends.friend:id,username,look',
'guilds.guild:id,name,badge',
'profileGuestbook.user:id,username,look',
'photos',
'photos.user:id,username,look',
'rooms',
'badges',
]);
+1 -1
View File
@@ -52,7 +52,7 @@ class WebsiteShopArticle extends Model
$furniture = json_decode($this->furniture, true);
$furnitureIds = array_column($furniture, 'item_id');
return ItemBase::query()->whereIn('id', $furnitureIds)->get();
return ItemBase::query()->whereIn('id', $furnitureIds)->select(['id', 'public_name'])->get();
}
public function rank(): HasOne
@@ -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();
}