Files
zulip/zephyr/lib/bulk_create.py
Tim Abbott d68cf3696b populate_db replay: Use bulk_create to create objects.
This cuts the time required to import 38000 messages (with 140000
message receipts) from about 10 minutes to just under 40 seconds on my
laptop with sqlite.

(imported from commit d53b0d1360408c77f38353b58fbb25875262cb40)
2012-10-20 18:26:18 -04:00

13 lines
570 B
Python

from django.conf import settings
# batch_bulk_create should become obsolete with Django 1.5, when the
# Django bulk_create method accepts a batch_size directly.
def batch_bulk_create(cls, cls_list, batch_size=150):
if "sqlite" not in settings.DATABASES["default"]["ENGINE"]:
# We only need to do the below batching nonsense with sqlite.
cls.objects.bulk_create(cls_list)
return
while len(cls_list) > 0:
current_batch = cls_list[0:batch_size]
cls.objects.bulk_create(current_batch)
cls_list = cls_list[batch_size:]