Skip to content

Batch ContentSummaryLog queries in _consolidate_courses_data#14444

Open
devswithme wants to merge 1 commit intolearningequality:developfrom
devswithme:optimize/batch-course-progress-summary-queries
Open

Batch ContentSummaryLog queries in _consolidate_courses_data#14444
devswithme wants to merge 1 commit intolearningequality:developfrom
devswithme:optimize/batch-course-progress-summary-queries

Conversation

@devswithme
Copy link
Copy Markdown

Summary

Reduces database queries in the learn plugin at _consolidate_courses_data from 2N to N+1, where N is the number of courses a learner is enrolled in. Previously, for each course node the function issued two separate queries: one COUNT for non-topic descendants and one COUNT on ContentSummaryLog for completed content. On a learner's home page or course list with 10 enrolled courses, this produced 20 queries.

References

N/A — standalone performance improvement, no related issue.

Reviewer guidance

  • The only changed function is _consolidate_courses_data in
    kolibri/plugins/learn/viewsets.py.
  • Run pytest kolibri/plugins/learn/test/test_learner_course.py and
    pytest kolibri/plugins/learn/test/test_learner_classroom.py to
    confirm all existing tests pass.
  • No risky areas: logic is equivalent, only query count changes.

AI usage

I used Claude to identify the N+1 query pattern and propose the batching approach and reviewed the generated code for correctness specifically verifying UUID type consistency between ContentNode and ContentSummaryLog, the double-iteration of the course_nodes query set, and the empty-set guard and confirmed the change produces identical output across all existing test cases before accepting it.

@rtibbles
Copy link
Copy Markdown
Member

Hi @devswithme - could you open an issue for this so we can discuss further before jumping to a solution? I agree with the assessment that there is some potential for optimization here, but I'd rather plan it out a bit more carefully.

@devswithme
Copy link
Copy Markdown
Author

Yes can, Thank you @rtibbles.

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.

2 participants