-
-
Notifications
You must be signed in to change notification settings - Fork 100
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(CommonPlayersStrategy): add index to reduce suggestions bottleneck #3132
Merged
wescopeland
merged 4 commits into
RetroAchievements:master
from
wescopeland:common-players-strategy-perf
Jan 28, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
27cdb09
perf(CommonPlayersStrategy): add index to reduce suggestions bottleneck
wescopeland 3b4901f
fix: address feedback
wescopeland b4beaa3
fix: address feedback
wescopeland c5e4e1d
Merge branch 'master' into common-players-strategy-perf
wescopeland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
database/migrations/2025_01_25_000000_update_player_games_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::table('player_games', function (Blueprint $table) { | ||
$table->dropIndex('player_games_completion_sample_idx'); | ||
|
||
$table->index([ | ||
'user_id', | ||
'achievements_unlocked', | ||
'achievements_total', | ||
'game_id', | ||
], 'player_games_suggestions_index'); // custom name needed because the auto-generated one is too long | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('player_games', function (Blueprint $table) { | ||
$table->dropIndex('player_games_suggestions_index'); | ||
|
||
$table->index( | ||
[ | ||
'game_id', | ||
'achievements_unlocked', | ||
'achievements_total', | ||
'user_id', | ||
'id', | ||
], | ||
'player_games_completion_sample_idx' // custom name needed because the auto-generated one is too long | ||
); | ||
}); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like the
player_games_completion_sample_idx
, but with the additionaldeleted_at
column. Do we need both?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's currently 0 deleted player_games records.
Maybe you could remove the
deleted_at
check form the query and leverage the existing index? A little deleted data isn't going to matter for the outcome of random selection.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's unfortunately still needed:
The screenshot above is with the index removing that column. Eloquent deciding to automagically include the column is what necessitates it being part of the compound index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
->withTrashed()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works - fixed in latest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so. As soon as I delete it, I see these monster queries appear:
![Screenshot 2025-01-26 at 10 59 31 AM](https://private-user-images.githubusercontent.com/3984985/406744906-ac2cd76a-4f99-45fe-a432-0cc0aef8c75d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2Nzg2NjAsIm5iZiI6MTczOTY3ODM2MCwicGF0aCI6Ii8zOTg0OTg1LzQwNjc0NDkwNi1hYzJjZDc2YS00Zjk5LTQ1ZmUtYTQzMi0wY2MwYWVmOGM3NWQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTZUMDM1OTIwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjNiZWQ4NjRlYmYyYjk1YWRlMDAxYzM3NTYwMGQyYjk3NDI0NGVhYzgzZWMzMThlMjJjZDUxODJkMzU0Njk4MyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.x2rLtDNcjL2v6hYb_8vRAgeNZN5y8-zVKnnmiENlPaI)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not leveraging the old index, there's no reason to remove the
deleted_at
clause.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm happy to implement feedback, but I'm getting confused. What specifically would you like me to do?
My interpretation is you'd like me to add back
deleted_at
to the index as well as the queries, but we don't have any deleted records.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping you'd be able to use the existing
player_games_completion_sample_idx
index, but it sounds like that's not being picked up.I see now that that index has
id
in it - why would you ever want id in an index? Maybe we can drop it and replace it with yours.If you're going to have to add a new index, do whatever you feel is necessary with it - like filtering out the deleted items.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
player_games_completion_sample_idx
is a covering index, butid
is probably redundant.Based on what I'm seeing in my local, we don't even need that index anymore and a lot of the code in the strategy can be simplified.
I've updated the new migration and the strategy in latest.