This is from the django docs on the queryset iterator()
method:
A QuerySet typically caches its results internally so that repeated evaluations do not result in additional queries. In contrast, iterator() will read results directly, without doing any caching at the QuerySet level (internally, the default iterator calls iterator() and caches the return value). For a QuerySet which returns a large number of objects that you only need to access once, this can results in better performance and a significant reduction in memory.
After reading, I'm still confused: The line about increased performance and memory reduction suggests we should just use the iterator()
method. Can someone give some examples of good and bad cases iterator()
usage?
Even if the query results are not cached, if they really wanted to access the models more than once, can't someone just do the following?
saved_queries = list(Model.objects.all().iterator())