Skip to content
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

perf: admin perf optimization for course-discovery #4496

Merged
merged 1 commit into from
Dec 4, 2024

Conversation

AfaqShuaib09
Copy link
Contributor

@AfaqShuaib09 AfaqShuaib09 commented Nov 26, 2024

PROD-4225

This PR adds the get_queryset method to Django Admin registered models. The aim is to optimize database queries and reduce query counts wherever possible by customizing the way queries are fetched in the admin interface. It will avoid unnecessary data fetching and reduce overhead.

Screenshots

Before:
image

After:
image

Before:
image

After:
image

Before:
image

After:
image

and so on...

@AfaqShuaib09 AfaqShuaib09 marked this pull request as ready for review November 29, 2024 06:38
Prefetch(
'type__translations',
queryset=ProgramTypeTranslation.objects.filter(language_code='en'),
to_attr='prefetched_translations'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Prefetch helpful? I don't see how the prefetched_translations attribute would be accessed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this will cause extra queries to fetch options in dropdown on retrieving a single object
image

Copy link
Contributor Author

@AfaqShuaib09 AfaqShuaib09 Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I've overridden the formfield_for_foreignkey method now to eliminate the similar queries

@@ -661,6 +739,14 @@ class PersonAdmin(admin.ModelAdmin):
readonly_fields = ('uuid',)
search_fields = ('uuid', 'salutation', 'family_name', 'given_name', 'slug',)

def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset = queryset.select_related(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than assigning queryset multiple times, it would be better to chain things like return super.get_queryset.select_related(...)...

queryset = super().get_queryset(request)
queryset = queryset.prefetch_related(
Prefetch('translations', queryset=LevelTypeTranslation.objects.all(), to_attr='translated_names')
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[style] Same comment as above

@AfaqShuaib09 AfaqShuaib09 force-pushed the afaq/prod-4225 branch 2 times, most recently from 7b245bc to 4de5a65 Compare December 4, 2024 07:22
@AfaqShuaib09 AfaqShuaib09 merged commit 8fc14fd into master Dec 4, 2024
14 checks passed
@AfaqShuaib09 AfaqShuaib09 deleted the afaq/prod-4225 branch December 4, 2024 07:53
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.

3 participants