From 0006615b99738cb77b3574e4050005d50e9ce349 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sat, 17 Feb 2024 12:04:09 +0100 Subject: [PATCH] other optimizations --- app/Http/ViewModels/Home/HomeViewModel.php | 20 ++++++++++++++----- .../2024_02_17_091516_name_index.php | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Http/ViewModels/Home/HomeViewModel.php b/app/Http/ViewModels/Home/HomeViewModel.php index 6a31963..adaf782 100644 --- a/app/Http/ViewModels/Home/HomeViewModel.php +++ b/app/Http/ViewModels/Home/HomeViewModel.php @@ -36,9 +36,12 @@ public static function twentyMostPopularNames(): array ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); - $randomNames = Name::where('name', '!=', '_PRENOMS_RARES') + $randomIds = Name::select('id') + ->where('name', '!=', '_PRENOMS_RARES') ->inRandomOrder() ->take(10) + ->get(); + $randomNames = Name::whereIn('id', $randomIds) ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); @@ -53,10 +56,13 @@ public static function twentyMostPopularNames(): array public static function nameSpotlight(): array { $name = Cache::remember('name-of-the-day', 86400, function () { - return Name::where('total', '>', 10000) - ->select('id', 'name', 'origins') + $id = Name::select('id') + ->where('total', '>', 10000) ->inRandomOrder() ->first(); + return Name::whereIn('id', $id) + ->select('id', 'name', 'origins') + ->first(); }); return [ @@ -86,10 +92,14 @@ public static function serverStats(): array */ public static function adminLists(): Collection { - return NameList::where('is_public', true) + $ids = NameList::select('id') + ->where('is_public', true) + ->inRandomOrder() + ->get(); + + return NameList::whereIn('id', $ids) ->withCount('names') ->with('names') - ->inRandomOrder() ->get() ->map(fn (NameList $list) => [ 'id' => $list->id, diff --git a/database/migrations/2024_02_17_091516_name_index.php b/database/migrations/2024_02_17_091516_name_index.php index 6212348..d07558f 100644 --- a/database/migrations/2024_02_17_091516_name_index.php +++ b/database/migrations/2024_02_17_091516_name_index.php @@ -12,7 +12,8 @@ public function up(): void { Schema::table('names', function (Blueprint $table) { - $table->index(['gender', 'name']); + $table->index(['gender', 'name', 'unisex']); + $table->index(['total']); }); }