I’m building a Django application at work that performs several thousand inserts. I was frustrated when it was taking several minutes to populate the database. For reference, I’m running on a Windows XP box with a 2GHz Xeon, 4GB of RAM, and I’m using SQLite as the database. In one straightforward piece of the program, it was taking over 3 minutes to insert 5000 rows.
Since I’m new to Django, I wasn’t immediately aware that, by default, every call to an ORM object’s save() results in a separate transaction. When you’re doing thousands of inserts, the overhead is unbearable. So after reading this, this, and this, I realized that the @transaction.commit_on_success decorator would allow me to lump a collection of saves into a single transaction.
from django.db import transaction
@transaction.commit_on_success
def myfunction(request):
for i in range(5000):
myobj = MyObject(mynumber=i)
myobj.save()
After I made this change, the 5000 inserts took roughly 15 seconds instead of over 3 minutes.
Bartek said
Very useful, thanks!
Chris Lasher said
Great tip! Thanks!