# Migration of large amount of data from one app to another

**URL:** <https://forum.djangoproject.com/t/migration-of-large-amount-of-data-from-one-app-to-another/1526>\
**Category:** Using Django\
**Created:** [March 18, 2020, 6:17am UTC](https://forum.djangoproject.com/t/migration-of-large-amount-of-data-from-one-app-to-another/1526 "2020-03-18T06:17:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eldary89](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eldary89](https://forum.djangoproject.com/u/Eldary89)\
**Post date:** [March 18, 2020, 6:17am UTC](https://forum.djangoproject.com/t/migration-of-large-amount-of-data-from-one-app-to-another/1526/1 "2020-03-18T06:17:10Z")

</div>

Hi, everyone! I am handling this problem. I should forward all recordings from one model to another. There I have about 2 million recordings with about 20 columns. Here’s my code:

def forward(frm, to, model, apps, schema\_editor):

> ```
> old_model = apps.get_model(frm, model)
> new_model = apps.get_model(to, model)
> list_fields = [i.get_attname() for i in old_model._meta.fields]
> qs = old_model.objects.all().order_by('created')
> new_model.objects.all().delete()
> i = 0
> j = 10000
> q = 0
> no = new_model()
> with transaction.atomic():
> while qs.last() != q:
> qs_part = qs[i:j]
> for item in qs_part:
> for field in list_fields:
> val = getattr(item, field)
> setattr(no, field, val)
> no.save()
> print("{} in {} forwarded".format(qs_part.count(), model))
> q = qs_part[qs_part.count()-1]
> i += j
> j += j
> print("{} is ok at {}".format(model, datetime.datetime.now()))
> 
> ```

Every time I get this error message:

> return self.cursor.execute(sql, params)  
> psycopg2.OperationalError: server closed the connection unexpectedly  
> This probably means the server terminated abnormally  
> before or while processing the request.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 18, 2020, 6:51pm UTC](https://forum.djangoproject.com/t/migration-of-large-amount-of-data-from-one-app-to-another/1526/2 "2020-03-18T18:51:17Z")

</div>

There are a number of ways to speed this up.

First, the fastest way would be to use raw SQL using the `INSERT INTO .. AS SELECT ...` statement. You might still need a chunked statement, but do this by PK - see below.

If you want to stay in ORM land, there are several things there. You shouldn’t use a single `atomic()` around the whole. A large trnasaction on the database server will consume a lot of resources. You can also avoid the `[i:j]` slice pattern, and instead use `filter(pk __gte=n, pk__ lt=n + chunk_size)`, iterating from the 0 to the highest PK value. I implemented a version of such iteration [in Django-MySQL](https://django-mysql.readthedocs.io/en/latest/queryset_extensions.html#smart-iteration), haven’t translated it to PostgreSQL yet though. You can use `bulk_create()` to greatly speed up model execution. You can avoid repeating the `qs_part.count()` queries. And you can also increase the server query timeout, although it’d be best to get an estimate of how long the whole process will take before you try that.
