Memory efficient Django Queryset Iterator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import gc

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
    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()

More like this

  1. Automatically generate admin by WoLpH 5 months, 2 weeks ago
  2. Batch querysets by jkocherhans 4 years, 6 months ago
  3. streaming serializer by kcarnold 4 years, 1 month ago
  4. Queryset Foreach by kcarnold 4 years, 1 month ago
  5. Server Side Cursors for Django's psycopg2 Backend by ryanbutterfield 2 years, 2 months ago

Comments

Romain Hardouin (on March 10, 2010):

Interesting

#

guettli (on April 15, 2010):

Django does not load all rows in memory, but it caches the result while iterating over the result. At the end you have everything in memory (if you don't use .iterator()). For most cases this is no problem.

I had memory problems when looping over huge querysets. I solved them with this:

Check connection.queries is empty. settings.DEBUG==True will store all queries there. (Or replace the list with a dummy object, which does not store anything):

from django.db import connection
assert not connection.queries, 'settings.DEBUG=True?'

Use queryset.iterator() to disable the internal cache.

Use values_list() if you know you need only some values.

#

barthed (on September 8, 2010):

Thanks. It saved my day!

The only issue I encountered is that the function throws an exception when the queryset is empty (no result).

#

tomgruner (on November 17, 2010):

Thanks, this worked great for a table I have with 250,000 rows in it!

#

(Forgotten your password?)