Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do transactions add overhead to the DB?

Would it add overhead to put a DB transactions around every single service method in our application?

We currently only use DB transactions where it's an explicit/obvious necessity. I have recently suggested transactions around all service methods, but some other developers asked the prudent question: will this add overhead?

My feeling is not - auto commit is the same as a transaction from the DB perspective. But is this accurate?

DB: MySQL

like image 216
David Parks Avatar asked Feb 14 '12 03:02

David Parks


People also ask

How does transactions work at database level?

A database transaction delimits a set of database operations (i.e. SQL statements), that are processed as a whole. Database operations included inside a transaction are validated or canceled as a unique operation. The database server is in charge of data concurrency and data consistency .

What is overhead in database?

In computer science, overhead is any combination of excess or indirect computation time, memory, bandwidth, or other resources that are required to perform a specific task.

Are SQL transactions expensive?

See Using Row Versioning-based Isolation Levels. So the conclusion is simple: transactions have no cost.

Why are transactions important to databases?

Benefits of Transactions Many database uses require storing data to multiple tables, or multiple rows to the same table in order to maintain a consistent data set. Using transactions ensures that other connections to the same database see either all the updates or none of them.


1 Answers

You are right, with autocommit every statement is wrapped in transaction. If your service methods are executing multiple sql statements, it would be good to wrap them into a transaction. Take a look at this answer for more details, and here is a nice blog post on the subject.

And to answer your question, yes, transactions do add performance overhead, but in your specific case, you will not notice the difference since you already have autocommit enabled, unless you have long running statements in service methods, which will cause longer locks on tables participating in transactions. If you just wrap your multiple statements inside a transaction, you will get one transaction (instead of transaction for every individual statement), as pointed here ("A session that has autocommit enabled can perform a multiple-statement transaction by starting it with an explicit START TRANSACTION or BEGIN statement and ending it with a COMMIT or ROLLBACK statement") and you will achieve atomicity on a service method level...

At the end, I would go with your solution, if that makes sense from the perspective of achieving atomicity on a service method level (which I think that you want to achieve), but there are + and - effects on performance, depending on your queries, requests/s etc...

like image 104
Aleksandar Vucetic Avatar answered Sep 28 '22 15:09

Aleksandar Vucetic