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(game): add composite indexes for game_id and achievements unlocked #1956

Merged

Conversation

wescopeland
Copy link
Member

This dramatically improves performance on the two queries to fetch the game's achievement distribution on game pages.

Benchmark query:

-- softcore mode, use NOW() to bypass local performance caching
SELECT
	NOW(),
	pg.achievements_unlocked AS AwardedCount,
	COUNT(*) AS NumUniquePlayers
FROM
	player_games AS pg
WHERE
	pg.game_id = 228
	AND pg.achievements_unlocked > 0
GROUP BY
	AwardedCount
ORDER BY
	AwardedCount DESC

Before
4.389s
4.697s
4.282s
4.723s
4.867s
Average: 4.591s

After
38ms
23ms
8ms
24ms
19ms
Average: 22.4ms

The addition of these indices results in a speedup factor of around 535x. This is especially desirable given the query runs twice on every game page.

@wescopeland wescopeland requested a review from a team October 30, 2023 02:05
@Jamiras
Copy link
Member

Jamiras commented Oct 30, 2023

I'm not sure if the NOW() is enough to bypass my local caching.

Before: 69ms, 71ms, 67ms, 70ms, 68ms (average: 69ms)
After: 8ms, 9ms, 8ms, 8ms, 8ms (average: 8ms)

Still a 8x improvement.

For 1446:

Before: 149ms, 78ms, 116ms, 94ms, 230ms (average: 133ms)
After: 5ms, 5ms, 8ms, 7ms, 6ms (average: 6ms)

That's a 22x improvement

@luchaos
Copy link
Member

luchaos commented Oct 30, 2023

runs twice on every game page

Should probably be pre-aggregated on games/sets. Won't scale well in the future.
The index does its job though, very nice!

@luchaos luchaos merged commit 5f59c24 into RetroAchievements:master Oct 30, 2023
5 checks passed
@wescopeland wescopeland deleted the achievement-distribution-indexes branch October 30, 2023 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants