Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve performances #36

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
}
};
Loading