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.
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.
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.
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.
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.
No. The purpose of database transactions is to ensure data integrity. See Wikipedia for more information.
Yes. Using transactions won't affect this.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With