Reputation: 454
I have a large dataset returned from django queryset and I want to iterate over it. Should I directly iterate over the queryset or store the results in a variable and iterate over it?
for item in Model.objects.all():
do_something()
or
results = Model.objects.all():
for item in results:
do_something()
As far as I know, the variables are stored in heap and its safer, where as in case of iterating over queryset, the results will be stored in main memory.
So which one is efficient in space and speed?
Upvotes: 17
Views: 33285
Reputation: 599490
There is no difference. Python does not distinguish between data on the heap and "main memory" (or the stack); in CPython at least, all data is stored on the heap, and the stack contains references to that data. See this question.
The only consideration here is whether you need to refer to the queryset again in the same scope. If you do, store it in a variable; if not, then there is no need to.
Upvotes: 21
Reputation: 10312
If your dataset is huge you could use iterator
to reduce memory load and improve performance, e.g.
results = Model.objects.all()
for item in results.iterator():
do_something()
You should only do this if it's really necessary, as it disables queryset caching. Re-using the queryset after this will result in poorer performance.
Upvotes: 15