Skip to content

Commit

Permalink
perf(feed): speed up slow query (#3015)
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Jan 5, 2025
1 parent 3552b9e commit e15cc3d
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions app/Community/Actions/BuildDeveloperFeedDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,12 @@ function ($join) {
*/
private function getRecentLeaderboardEntries(User $targetUser): array
{
$thirtyDaysAgo = Carbon::now()->subDays(30);

// Using FORCE INDEX in MySQL/MariaDB dramatically improves performance (from ~550ms to ~20ms).
// We conditionally apply the hint only when using MySQL/MariaDB. It is not supported by SQLite.
$query = LeaderboardEntry::query();

if (DB::connection()->getDriverName() === 'mariadb') {
$query->from(DB::raw('leaderboard_entries FORCE INDEX (idx_recent_entries)'));
}

return $query
->with(['leaderboard', 'leaderboard.game', 'leaderboard.game.system', 'user'])
->join('LeaderboardDef', 'LeaderboardDef.ID', '=', 'leaderboard_entries.leaderboard_id')
->where('LeaderboardDef.author_id', $targetUser->id)
return LeaderboardEntry::select('leaderboard_entries.*')
->with(['leaderboard.game.system', 'user'])
->join('LeaderboardDef as ld', 'ld.ID', '=', 'leaderboard_entries.leaderboard_id')
->where('ld.author_id', $targetUser->id)
->whereNull('leaderboard_entries.deleted_at')
->where('leaderboard_entries.updated_at', '>=', $thirtyDaysAgo)
->select('leaderboard_entries.*')
->where('leaderboard_entries.updated_at', '>=', now()->subDays(30))
->orderBy('leaderboard_entries.updated_at', 'desc')
->take(200)
->get()
Expand Down

0 comments on commit e15cc3d

Please sign in to comment.