Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving many Django objects with one big INSERT statement

Tags:

python

django

Imagine you have the following situation:

for i in xrange(100000):
  account = Account()
  account.foo = i
  account.save

Obviously, the 100,000 INSERT statements executed by Django are going to take some time. It would be nicer to be able to combine all those INSERTs into one big INSERT. Here's the kind of thing I'm hoping I can do:

inserts = []

for i in xrange(100000):
  account = Account()
  account.foo = i
  inserts.append(account.insert_sql)

sql = 'INSERT INTO whatever... ' + ', '.join(inserts)

Is there a way to do this using QuerySet, without manually generating all those INSERT statements?

like image 472
Jason Swett Avatar asked Apr 12 '26 08:04

Jason Swett


1 Answers

As shown in this related question, one can use @transaction.commit_manually to combine all the .save() operations as a single commit to greatly improve performance.

@transaction.commit_manually
def your_view(request):
    try:
        for i in xrange(100000):
            account = Account()
            account.foo = i
            account.save()   
    except: 
        transaction.rollback()
    else:
        transaction.commit() 

Alternatively, if you're feeling adventurous, have a look at this snippet which implements a manager for bulk inserting. Note that it works only with MySQL, and hasn't been updated in a while so it's hard to tell if it will play nice with newer versions of Django.

like image 166
Shawn Chin Avatar answered Apr 13 '26 21:04

Shawn Chin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!