Skip to content

Fix N+1 query in course sitemap generation#360

Merged
C4ptainCrunch merged 2 commits into
mainfrom
sitemap-perf
Jan 3, 2026
Merged

Fix N+1 query in course sitemap generation#360
C4ptainCrunch merged 2 commits into
mainfrom
sitemap-perf

Conversation

@C4ptainCrunch

Copy link
Copy Markdown
Contributor

No description provided.

claude and others added 2 commits January 3, 2026 21:25
Replace inefficient per-course document query with database aggregation
using Max() annotation. This reduces sitemap generation from N+1 queries
to just 1 query, where N is the number of courses.

Before:
- items() prefetched document_set for each course
- lastmod() called .order_by().first() for each course (N queries)
- For 1000 courses: 1001 queries

After:
- items() annotates with Max('document__created')
- lastmod() returns annotated field
- For 1000 courses: 1 query

This fix prevents database overload during sitemap generation and
significantly improves response time for sitemap endpoints.
@C4ptainCrunch C4ptainCrunch merged commit b622061 into main Jan 3, 2026
2 checks passed
@C4ptainCrunch C4ptainCrunch deleted the sitemap-perf branch January 3, 2026 20:36
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