Skip to content

Commit

Permalink
perf: improve performances (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 17, 2024
1 parent 5bcf456 commit 7ed125e
Show file tree
Hide file tree
Showing 3 changed files with 49 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
6 changes: 5 additions & 1 deletion app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ public static function jsonLdSchema(Name $name): array

public static function relatedNames(Name $name): Collection
{
return Name::where('name', '!=', '_PRENOMS_RARES')
$ids = Name::select('id')
->where('name', '!=', '_PRENOMS_RARES')
->where('id', '!=', $name->id)
->where('gender', $name->gender)
->inRandomOrder()
->take(10)
->get();

return Name::whereIn('id', $ids)
->get()
->map(fn (Name $name) => NameViewModel::summary($name));
}
Expand Down
29 changes: 29 additions & 0 deletions database/migrations/2024_02_17_091516_name_index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('names', function (Blueprint $table) {
$table->index(['gender', 'name', 'unisex']);
$table->index(['total']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('names', function (Blueprint $table) {
$table->dropIndex('names_gender_name_index');
});
}
};

0 comments on commit 7ed125e

Please sign in to comment.