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(feed): speed up slow query #3015

Merged
Merged
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
21 changes: 5 additions & 16 deletions app/Community/Actions/BuildDeveloperFeedDataAction.php
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
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))
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
->orderBy('leaderboard_entries.updated_at', 'desc')
->take(200)
->get()
Expand Down