Changes between Initial Version and Version 1 of Ticket #26780, comment 4


Ignore:
Timestamp:
Jun 20, 2016, 5:07:02 AM (8 years ago)
Author:
Ludwik Trammer

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #26780, comment 4

    initial v1  
    55> accessing category.example_posts[:3] would have the same effect
    66
    7 I'm pretty sure that is not correct. The end effect would obviously be the same, but the way it is achieved behind the scenes and the performance characteristics that follows would be widely different. What you propose (prefetching without limit, adding slicing in a loop after the fact) would make Django to perform a database query selecting '''all objects from the database table''' and loading them into memory. This would happen when the main queryset is evaluated (that's what prefetching is all about). Then the slicing would be perform by Python in memory, on a queryset that was already evaluated.
     7I'm pretty sure that is not correct. The end effect would obviously be the same, but the way it is achieved behind the scenes and the performance characteristics that follows would be widely different. What you propose (prefetching without limit, adding slicing in a loop after the fact) would make Django perform a database query selecting '''all objects from the database table''' and loading them into memory. This would happen when the main queryset is evaluated (that's what prefetching is all about). Then the slicing would be performed by Python in memory, on a queryset that was already evaluated.
    88
    99That's what I understood from [https://docs.djangoproject.com/en/1.9/ref/models/querysets/#prefetch-objects the documentation] and also how Django actually behaved in an experiment I performed couple of minutes ago. What I want to avoid is exactly this behavior - loading thousands of objects from the database to display first three of them.
Back to Top