Skip to content

Commit 68b40b9

Browse files
authored
Fix slow queries (#1350)
* Fix slow queries * Bump cache time * Refactor query
1 parent 286d549 commit 68b40b9

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

app/Http/Controllers/Forum/TagsController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Models\Tag;
88
use App\Models\Thread;
99
use App\Models\User;
10+
use Illuminate\Support\Facades\Cache;
1011
use Illuminate\View\View;
1112

1213
class TagsController extends Controller
@@ -35,8 +36,12 @@ public function show(Tag $tag): View
3536
}
3637

3738
$tags = Tag::orderBy('name')->get();
38-
$topMembers = User::mostSolutionsInLastDays(365)->take(5)->get();
39-
$moderators = User::moderators()->get();
39+
$topMembers = Cache::remember('topMembers', now()->addHour(), function () {
40+
return User::mostSolutionsInLastDays(365)->take(5)->get();
41+
});
42+
$moderators = Cache::remember('moderators', now()->addDay(), function () {
43+
return User::moderators()->get();
44+
});
4045
$canonical = canonical('forum.tag', [$tag->name, 'filter' => $filter]);
4146

4247
return view('forum.overview', compact('threads', 'filter', 'tags', 'topMembers', 'moderators', 'canonical') + ['activeTag' => $tag]);

app/Http/Controllers/Forum/ThreadsController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public function overview()
6161
}
6262

6363
$tags = Tag::orderBy('name')->get();
64-
$topMembers = Cache::remember('topMembers', now()->addMinutes(30), function () {
64+
$topMembers = Cache::remember('topMembers', now()->addHour(), function () {
6565
return User::mostSolutionsInLastDays(365)->take(5)->get();
6666
});
67-
$moderators = Cache::remember('moderators', now()->addMinutes(30), function () {
67+
$moderators = Cache::remember('moderators', now()->addDay(), function () {
6868
return User::moderators()->get();
6969
});
7070
$canonical = canonical('forum', ['filter' => $filter]);
@@ -74,7 +74,7 @@ public function overview()
7474

7575
public function show(Thread $thread)
7676
{
77-
$moderators = Cache::remember('moderators', now()->addMinutes(30), function () {
77+
$moderators = Cache::remember('moderators', now()->addDay(), function () {
7878
return User::moderators()->get();
7979
});
8080

app/Models/Thread.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static function feedByTagQuery(Tag $tag): Builder
295295
public static function feedQuery(): Builder
296296
{
297297
return self::query()
298-
->withCount(['repliesRelation as reply_count', 'likesRelation as like_count'])
298+
->select('threads.*')
299299
->latest('last_activity_at');
300300
}
301301

app/Models/User.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Eloquent\Factories\HasFactory;
1212
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1313
use Illuminate\Database\Eloquent\Relations\HasMany;
14+
use Illuminate\Database\Query\JoinClause;
1415
use Illuminate\Foundation\Auth\User as Authenticatable;
1516
use Illuminate\Notifications\Notifiable;
1617
use Illuminate\Support\Facades\Auth;
@@ -340,19 +341,23 @@ public function countSolutions(): int
340341

341342
public function scopeMostSolutions(Builder $query, ?int $inLastDays = null)
342343
{
343-
return $query->withCount(['replyAble as solutions_count' => function ($query) use ($inLastDays) {
344-
$query->where('replyable_type', 'threads')
345-
->join('threads', function ($join) {
346-
$join->on('threads.solution_reply_id', '=', 'replies.id')
347-
->on('threads.author_id', '!=', 'replies.author_id');
348-
});
349-
350-
if ($inLastDays) {
351-
$query->where('replies.created_at', '>', now()->subDays($inLastDays));
352-
}
353-
354-
return $query;
355-
}])
344+
return $query
345+
->selectRaw('users.*, COUNT(DISTINCT replies.id) AS solutions_count')
346+
->join('replies', 'replies.author_id', '=', 'users.id')
347+
->join('threads', function (JoinClause $join) {
348+
$join->on('threads.solution_reply_id', '=', 'replies.id')
349+
->on('threads.author_id', '!=', 'replies.author_id');
350+
})
351+
->where(function ($query) use ($inLastDays) {
352+
$query->where('replyable_type', 'threads');
353+
354+
if ($inLastDays) {
355+
$query->where('replies.created_at', '>', now()->subDays($inLastDays));
356+
}
357+
358+
return $query;
359+
})
360+
->groupBy('users.id')
356361
->having('solutions_count', '>', 0)
357362
->orderBy('solutions_count', 'desc');
358363
}

0 commit comments

Comments
 (0)