Last active
April 27, 2016 12:57
-
-
Save TauPan/40bf506b4542d6580dc92b305f516a3b to your computer and use it in GitHub Desktop.
My Version of queryset_iterator from https://djangosnippets.org/snippets/1949/ (adds .exists() in the beginning and resetting db queries in debug mode)
This file contains hidden or 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
from django import db | |
import gc | |
# from https://djangosnippets.org/snippets/1949/ | |
def queryset_iterator(queryset, chunksize=1000): | |
''''' | |
Iterate over a Django Queryset ordered by the primary key | |
This method loads a maximum of chunksize (default: 1000) rows in it's | |
memory at the same time while django normally would load all rows in it's | |
memory. Using the iterator() method only causes it to not preload all the | |
classes. | |
Note that the implementation of the iterator does not support ordered query | |
sets. | |
''' | |
pk = 0 | |
if queryset.exists(): | |
last_pk = queryset.order_by('-pk')[0].pk | |
queryset = queryset.order_by('pk') | |
while pk < last_pk: | |
for row in queryset.filter(pk__gt=pk)[:chunksize]: | |
pk = row.pk | |
yield row | |
gc.collect() | |
db.reset_queries() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment