2017-06-22 17 views
1

大規模なデータセットを反復するためにクエリーセットイテレータを使用したいと思います。 Djangoはiterator()を提供していますが、それは各繰り返しでデータベースにヒットします。チャンク内の反復のためのコードを見つけました -Django Queryset Iterator for Ordered Queryset

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').values_list('pk', flat=True).first() 
    if last_pk is not None: 
     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() 

これは順序付けされていないクエリーセットで機能します。順序付けされたクエリーセットでこれを行うためのソリューション/回避策はありますか?

答えて

1

ここは私のもので、ソート機能があります。

ところで、あなたが使用しているイテレーターは、処理中にクエリーセット項目が変更された場合、「永遠のループ」を持っています。

そして、イテレータの下last_pk

def queryset_iterator(queryset, chunksize=10000, key=None): 
    key = [key] if isinstance(key, basestring) else (key or ['pk']) 
    counter = 0 
    count = chunksize 
    while count == chunksize: 
     offset = counter - counter % chunksize 
     count = 0 
     for item in queryset.all().order_by(*key)[offset:offset + chunksize]: 
      count += 1 
      yield item 
     counter += count 
     gc.collect() 
に無駄なクエリを持っていません
関連する問題