Skip to content

Commit

Permalink
other optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Feb 17, 2024
1 parent c5edeb3 commit 0006615
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 15 additions & 5 deletions app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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 [
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/2024_02_17_091516_name_index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
}

Expand Down

0 comments on commit 0006615

Please sign in to comment.