-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Using `iterator()` to loop through large querysets efficiently | ||
|
||
## Links | ||
|
||
- [`iterator()` in the Django docs](https://docs.djangoproject.com/en/5.0/ref/models/querysets/#iterator) | ||
|
||
## Use case | ||
|
||
I learned this when I reviewed a PR from my colleague Jeff. We needed to loop through about 40k records to reset some data. | ||
|
||
> 💡 Side note: This is one of the reasons PR review is helpful -- cross-training! | ||
From the docs: | ||
|
||
> 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 result in better performance and a significant reduction in memory. | ||
## Code example | ||
|
||
```py | ||
from my_app.models import MyModel | ||
|
||
# Perform whatever query you need | ||
qs = MyModel.objects.all() | ||
|
||
# Loop over it with `iterator` | ||
for obj in qs.iterator(): | ||
# whatever you needed to do in this loop | ||
``` |