Changes between Initial Version and Version 1 of Ticket #26780, comment 4
- Timestamp:
- Jun 20, 2016, 5:07:02 AM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #26780, comment 4
initial v1 5 5 > accessing category.example_posts[:3] would have the same effect 6 6 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 performby Python in memory, on a queryset that was already evaluated.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 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. 8 8 9 9 That'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.