Django-MPTT maintains a tree structure for you. Thus, with each insert_at it will change all the nodes to the right of the inserted one - this is why you are experiencing performance issues.
One way is to manually build a tree structure without django-mptt .
So, you will need to take new entries, and, according to them, find out how the old nodes in the tree should be changed. Since you only insert data, only the left and right attributes are changed, but not the level, so this should make it a little easier. Once you know which nodes will be changed, you can change them using a single update transaction.
Then you can start inserting new data. Again, the fastest way is to calculate the left, right and level values ββfor each new record, and then do one bulk_insert (Django> = 1.4). Doing this will only result in two db operations, which obviously should be much faster in terms of db transactions.
However, this method will require some clever way to figure out how to change the old nodes in the tree. The easiest way is to unload the entire tree into a python structure, and then figure out the changes in that structure. This will not be possible if your tree is very large due to memory limitations.
Now I'm not sure if there is a more efficient way to do this. Maybe someone else at StackOverflow has some interesting ideas ...
EDIT
Sorry for the confusion of update . I meant one transaction. In such cases, I usually make a raw sql query, where I do update tbname set ... where id=1; update tbname set ... where id=2; update tbname set ... where id=1; update tbname set ... where id=2; Therefore, I am doing several sql statements in a single sql query. In my experience, the expensive part of db is not the execution of the statement, but the transaction itself, since there is network latency, db blocking, etc. Thus, having one transaction allows db to be as fast as possible. Not sure how to do this in django using queries. I usually make a raw SQL query.