-
Notifications
You must be signed in to change notification settings - Fork 3
Optimize N+1 Queries Across API Endpoints #3333
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
base: main
Are you sure you want to change the base?
Changes from all commits
1065bdd
8bb75b8
0aa5955
944419d
803a068
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -337,7 +337,12 @@ def filter_queryset(self, queryset): | |
|
|
||
| if "courserun_is_enrollable" not in filter_keys: | ||
| queryset = queryset.prefetch_related( | ||
| Prefetch("courseruns", queryset=CourseRun.objects.order_by("id")), | ||
| Prefetch( | ||
| "courseruns", | ||
| queryset=CourseRun.objects.order_by("id").prefetch_related( | ||
| "products" | ||
| ), | ||
| ), | ||
| ) | ||
|
Comment on lines
339
to
346
|
||
|
|
||
| return queryset | ||
|
|
||
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.
Prefetching
contract_programshere won’t eliminate the N+1 inContractPageSerializer.get_programs, because that method callsinstance.programs(a@propertythat runs a freshProgram.objects.filter(...)query per contract). To actually reduce queries, prefetch the relationship that the property uses (e.g.,contract_programs__program) and/or change the serializer to read program IDs from the prefetchedcontract_programsitems instead of callinginstance.programs.