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?