Search code examples
djangodatabasetransactions

Do transaction in Django make things faster


I have a Django application.

This Django application makes an api call to Twitter and gets my 100 recent tweets. I need to insert the tweets into the database.

I have a method to act on all 100 tweets. This method has a for loop where I create a model instance out of each Tweet and call instance.save() on each instance separately.

I found things slower and thought that transactions might make things faster.

So outside the method I added @method_decorator(transaction.atomic). I still have a for loop where I created model instance and do instance.save() but now the method is decorated with transaction.atomic.

  1. Is transaction.atomic supposed to make insertions to database faster for me?
  2. Will each instance.save() call still issue a database call?
  3. If it makes things faster, then how?

Solution

  • In an atomic transaction, a series of database operations either all occur, or nothing occurs.

    Decorating a method as atomic will guarantee atomicity on the database for the block of code within that method. If the method is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.

    1. Transactions will not make your code any faster as per your use case. Infact, it will just guarantee that either all the tweets will be saved to the database or none of the tweets will be saved in the case of an exception.

    2. Yes, each instance.save() call will be a database call. So, there would be 100 database calls each time.

    3. Again, Transactions won't make this any faster for your use case.

    How to make things faster then?

    You can use Django's bulk_create to create all the tweets in a single query.

    Tweet.objects.bulk_create([tweet1, tweet2, ...., tweet100]) # single query
    

    This inserts the list of tweet objects into the database in an efficient manner in only 1 query.