fix: eliminate N+1 queries in ContestList scoreboard visibility check#550
Open
whlongg wants to merge 2 commits intoVNOI-Admin:masterfrom
Open
fix: eliminate N+1 queries in ContestList scoreboard visibility check#550whlongg wants to merge 2 commits intoVNOI-Admin:masterfrom
whlongg wants to merge 2 commits intoVNOI-Admin:masterfrom
Conversation
- author_ids / editor_ids: use _prefetched_objects_cache when authors/curators are already prefetched, avoiding duplicate through-table queries per contest. - can_see_full_scoreboard: check prefetch cache before calling view_contest_scoreboard.filter(...).exists() to avoid per-contest DB hit. - ContestList._get_queryset: add view_contest_scoreboard to prefetch_related. - ContestList.get_context_data: also prefetch contest__view_contest_scoreboard for active participations. - Add judge/tests/test_contest_list_perf.py: regression guard asserting query count stays <= QUERY_BUDGET for 10 hidden-scoreboard contests.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Type of change: bug fix
What
Eliminates N+1 queries in
ContestListwhen checking scoreboard visibility for hidden-scoreboard contests (SCOREBOARD_HIDDEN/SCOREBOARD_AFTER_CONTEST).author_ids/editor_ids: check_prefetched_objects_cache(viagetattr) before falling back to through-table queries, so prefetchedauthors/curatorsdata is reused.can_see_full_scoreboard: use Python-sideany()over prefetched data instead of.filter(...).exists()per contest.ContestList._get_queryset: addview_contest_scoreboardtoprefetch_related.ContestList.get_context_data: also prefetchcontest__view_contest_scoreboardfor active participations.Why
For each contest with a hidden scoreboard, Django was firing 3 extra queries per contest:
Contest.authors.through.objects.filter(contest=self)— bypasses prefetch cacheContest.curators.through.objects.filter(contest=self)— sameview_contest_scoreboard.filter(id=user.profile.id).exists()— not prefetched at allAll fallback paths are preserved for contexts without prefetch (admin, APIs, single-object views) using
getattr(self, '_prefetched_objects_cache', {}).Fixes #549
How Has This Been Tested?
Automated test —
judge/tests/test_contest_list_perf.py:SCOREBOARD_HIDDEN, authenticated user.connection.queries(DEBUG=True).Direct method benchmark —
can_see_own_scoreboard()across multiple scenarios (5 reps each):Before fix: 4 queries/contest (2× through-table + exists + has_completed_contest).
After fix: 1 query/contest (only
has_completed_contestremains, out of scope).judge.tests.test_contest_list_perf— query count bounded (27 ≤ 35, before fix was 46)Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the AGPL-3.0 License.