简体   繁体   中英

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM