MaxCore
MaxCore

Reputation: 2738

Django: prefetch_related grants performance only for non paginated requests?

For example, I have 1000 Users with lots of related objects that I use in template.

Is it right that this:

User.objects.all()[:10]

Will always perform better than this:

User.objects.all().prefetch_related('educations', 'places')[:10]

Upvotes: 0

Views: 198

Answers (1)

Alasdair
Alasdair

Reputation: 309039

This line will do an extra query to fetch the related objects for educations and places.

User.objects.all().prefetch_related('educations', 'places')[:10]

However it will only fetch the related objects for the sliced queryset User.objects.all()[:10], so you don't have to worry that it will fetch the related objects the thousands of other users in your database.

Upvotes: 1

Related Questions