Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 256
Akshar Raaj Avatar asked Jul 11 '15 08:07

Akshar Raaj


People also ask

What does transaction atomic do in Django?

Django provides a single API to control database transactions. Atomicity is the defining property of database transactions. atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database.

How does Django handle large data?

Use bulk query. Use bulk queries to efficiently query large data sets and reduce the number of database requests. Django ORM can perform several inserts or update operations in a single SQL query. If you're planning on inserting more than 5000 objects, specify batch_size.

How does Django handle concurrency?

In terms of Django, optimistic concurrency control can be implemented by overriding the save method on your model class... And, of course, for either of these concurrency mechanisms to be robust, you have to consider transactional control.

Are transactions Atomic?

SQL transactions, like transactions on all database platforms, put the data in isolation to cover the entire ACID acronym (atomic, consistent, isolated and durable). So the answer is yes.


2 Answers

  1. No. The purpose of database transactions is to ensure data integrity. See Wikipedia for more information.

  2. Yes. Using transactions won't affect this.

  3. It doesn't.

What should make your code faster is submitting your new records all at once, in a single query, rather than one at a time. Look into Django's bulk_create. It's always hard to predict performance, but I suspect that will noticeably improve things.

like image 37
Kevin Christopher Henry Avatar answered Sep 18 '22 03:09

Kevin Christopher Henry


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.

like image 88
Rahul Gupta Avatar answered Sep 21 '22 03:09

Rahul Gupta