Repository navigation
perf(grades): optimize database queries for large-scale grade recalcu… - #90
Merged
Merged
Conversation
andrey-canon
changed the base branch from
and/FUTUREX-1331
to
open-release/verawood.nelp
August 4, 2026 16:55
…lations
Replaced inefficient SQL OFFSET pagination with ID-based keyset pagination to ensure consistent lookup performance, and updated ordering to leverage the primary key index. Resolved an N+1 query issue by eagerly loading user data via `.select_related('user')` and optimized memory footprint using `.values_list()`.
These changes reduce execution time by ~25x and eliminate 100 redundant queries per batch during high-enrollment course processing.
andrey-canon
force-pushed
the
and/FUTUREX-1744
branch
from
August 4, 2026 17:41
6f84e74 to
5dc0978
Compare
andrey-canon
temporarily deployed
to
open-release/verawood.nelp
August 5, 2026 20:43 — with
GitHub Actions
Inactive
andrey-canon
temporarily deployed
to
open-release/verawood.nelp
August 5, 2026 20:43 — with
GitHub Actions
Inactive
This branch was previously deployed
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
This PR addresses significant platform performance degradation caused by inefficient database queries during large-scale course grade recalculations.
The previous implementation relied on SQL$N+1$ query problem by fetching user data individually for every enrollment in a batch.
OFFSETpagination, which forced the database to perform full index scans and discard hundreds of thousands of rows for high-offset tasks. Additionally, it suffered from anChanges
_course_task_argsandcompute_grades_for_courseto usestart_id(ID-based seeking) instead ofoffset. This ensures O(1) database lookup performance regardless of the course size.order_by('created')withorder_by('id')to leverage the Primary Key clustered index..select_related('user')to the enrollment QuerySet to fetch user data in a singleJOINquery, eliminating.values_list('id', flat=True)in the task generator to minimize memory footprint when handling courses with 400k+ enrollments.How to Test
Run the following script in the Django shell (
python manage.py lms shell) on a high-enrollment course:Performance Benchmarks
Issue # 1744
Migration pr of #73