From 3947da68ba51c6a2579ec90a997caf5fc7bfcc76 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 6 Mar 2013 09:52:39 +0000 Subject: [PATCH] slice update to reduce memory usage --- pandora/sequence/migrations/0002_optimize1.py | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pandora/sequence/migrations/0002_optimize1.py b/pandora/sequence/migrations/0002_optimize1.py index e2d1a8571..9771fab73 100644 --- a/pandora/sequence/migrations/0002_optimize1.py +++ b/pandora/sequence/migrations/0002_optimize1.py @@ -29,12 +29,20 @@ class Migration(SchemaMigration): # Adding unique constraint on 'Sequence', fields ['sort', 'start', 'end', 'mode'] db.create_unique('sequence_sequence', ['sort_id', 'start', 'end', 'mode']) - - - for s in orm['sequence.Sequence'].objects.all(): - s.mode2 = s.mode == 'color' and 1 or 0 - s.hash2 = int(s.hash, 16) - 9223372036854775808 - s.save() + + def nslice(s, n): + while len(s) >= n: + yield s[:n] + s = s[n:] + if len(s): + yield s + + ids = [s['id'] for s in orm['sequence.Sequence'].objects.all().values('id')] + for i in nslice(ids, 10000): + for s in orm['sequence.Sequence'].objects.filter(id__in=i): + s.mode2 = s.mode == 'color' and 1 or 0 + s.hash2 = int(s.hash, 16) - 9223372036854775808 + s.save() def backwards(self, orm): # Removing unique constraint on 'Sequence', fields ['sort', 'start', 'end', 'mode']