From e8361abbd402284c8697ce1648f97b33cd854db2 Mon Sep 17 00:00:00 2001 From: Wes Copeland Date: Wed, 6 May 2026 17:27:05 -0400 Subject: [PATCH 1/2] perf(UpdateGamePlayerCountAction): collapse player count queries --- .../Actions/UpdateGamePlayerCountAction.php | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/Platform/Actions/UpdateGamePlayerCountAction.php b/app/Platform/Actions/UpdateGamePlayerCountAction.php index d5ec102960..e01da505cb 100644 --- a/app/Platform/Actions/UpdateGamePlayerCountAction.php +++ b/app/Platform/Actions/UpdateGamePlayerCountAction.php @@ -16,7 +16,7 @@ public function execute(Game $game): void { $gameIds = [$game->id]; - $coreGameAchievementSet = $game->gameAchievementSets()->core()->first(); + $coreGameAchievementSet = $game->gameAchievementSets()->core()->with('achievementSet')->first(); if ($coreGameAchievementSet) { $bonusSubsetAchievementSets = GameAchievementSet::query() ->where('achievement_set_id', $coreGameAchievementSet->achievement_set_id) @@ -29,21 +29,26 @@ public function execute(Game $game): void } } - $playersQuery = PlayerGame::whereIn('game_id', $gameIds) - ->leftJoin('unranked_users', 'player_games.user_id', '=', 'unranked_users.user_id') - ->whereNull('unranked_users.id'); - - if (count($gameIds) > 1) { - $playersQuery->distinct('player_games.user_id'); - } + // multi-gameId path means parent + bonus subset; the same user can appear in both, + // so we COUNT(DISTINCT user_id) there instead of SUM-ing rows directly + $isMultiGame = count($gameIds) > 1; + $countExpr = fn (string $column): string => $isMultiGame + ? "COUNT(DISTINCT CASE WHEN player_games.{$column} > 0 THEN player_games.user_id END)" + : "SUM(CASE WHEN player_games.{$column} > 0 THEN 1 ELSE 0 END)"; - $game->players_total = $playersQuery->clone() - ->where('achievements_unlocked', '>', 0) - ->count(); + $row = PlayerGame::query() + ->whereIn('player_games.game_id', $gameIds) + ->leftJoin('unranked_users', 'player_games.user_id', '=', 'unranked_users.user_id') + ->whereNull('unranked_users.id') + ->selectRaw(sprintf( + '%s AS total, %s AS hardcore', + $countExpr('achievements_unlocked'), + $countExpr('achievements_unlocked_hardcore'), + )) + ->first(); - $game->players_hardcore = $playersQuery->clone() - ->where('achievements_unlocked_hardcore', '>', 0) - ->count(); + $game->players_total = (int) ($row->total ?? 0); + $game->players_hardcore = (int) ($row->hardcore ?? 0); if ($game->isDirty()) { $game->saveQuietly(); From 2cb873103dd6b97901ed86ac1879ed74e7613b3f Mon Sep 17 00:00:00 2001 From: Wes Copeland Date: Wed, 6 May 2026 17:31:37 -0400 Subject: [PATCH 2/2] chore: remove eager load --- app/Platform/Actions/UpdateGamePlayerCountAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Platform/Actions/UpdateGamePlayerCountAction.php b/app/Platform/Actions/UpdateGamePlayerCountAction.php index e01da505cb..04cb01e2d7 100644 --- a/app/Platform/Actions/UpdateGamePlayerCountAction.php +++ b/app/Platform/Actions/UpdateGamePlayerCountAction.php @@ -16,7 +16,7 @@ public function execute(Game $game): void { $gameIds = [$game->id]; - $coreGameAchievementSet = $game->gameAchievementSets()->core()->with('achievementSet')->first(); + $coreGameAchievementSet = $game->gameAchievementSets()->core()->first(); if ($coreGameAchievementSet) { $bonusSubsetAchievementSets = GameAchievementSet::query() ->where('achievement_set_id', $coreGameAchievementSet->achievement_set_id)