Commit e2e4b8a3 by Matjaz Gregoric

Avoid count(*) query in management command.

Count(*) is very slow on large tables.
parent c559a665
import math
import time
from django.core.management.base import BaseCommand
from problem_builder.models import Answer
......@@ -29,18 +28,21 @@ class Command(BaseCommand):
batch_size = options['batch_size']
sleep_time = options['sleep']
queryset = Answer.objects.filter(course_key__isnull=True)
batch_count = int(math.ceil(queryset.count() / float(batch_size)))
self.stdout.write(
"Copying Answer.course_id field into Answer.course_key in batches of {}".format(batch_size)
)
for batch in xrange(1, batch_count + 1):
for answer in queryset[:batch_size]:
idx = 0
while True:
idx += 1
batch = queryset[:batch_size]
if not batch:
break
for answer in batch:
answer.course_key = answer.course_id
answer.save()
self.stdout.write("Processed batch {} of {}".format(batch, batch_count))
if batch != batch_count:
time.sleep(sleep_time)
self.stdout.write("Processed batch {}".format(idx))
time.sleep(sleep_time)
self.stdout.write("Successfully copied Answer.course_id into Answer.course_key")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment