#26780: Prefetch objects don't work with slices -----------------------------------+-------------------------------------- Reporter: ludwik | Owner: nobody Type: Bug | Status: new Component: Uncategorized | Version: 1.9 Severity: Normal | Resolution: Keywords: prefetch, slicing | Triage Stage: Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -----------------------------------+--------------------------------------
Comment (by ludwik): Charettes, Thank you for the reply. > accessing category.example_posts[:3] would have the same effect 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. 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. I would be happy with a sane workaround, but to my knowledge there isn't any. -- Ticket URL: <https://code.djangoproject.com/ticket/26780#comment:4> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.230a9276ae27303d2f4230c0ae3ea0f8%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.