Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InnoDB optimization - "use transactions when doing updates" - why?

I was reading this post on the MySQL performance blog.

Under application tuning it says:

First make sure you’re using transactions when doing updates

I am currently only using transactions in places where there are multiple inserts or updates at the same time. i.e. more than one table.

Should I therefore alter every UPDATE to change it into a transaction?

What is the difference between:

prepare sql
bind params
commit

and:

begin transaction
prepare sql
bind params
execute statement
commit transaction

in terms of what happens at the database level, that makes one faster than the other?

like image 894
bcmcfc Avatar asked Dec 13 '10 12:12

bcmcfc


1 Answers

InnoDB runs in autocommit mode by default. What it means is that every query runs in it's own transaction and gets commited at once. In InnoDB this mean writing data into two places on disk (don't ask me for details - I'm writing from memory what I've read at MySQL Performance Blog once ;) ).

Now, if you do one update/insert/delete at a time, there's not much performance to gain. However, if you do several consecutive update/inserts/deletes, you can save some hard disk time by bundling them into transaction, and then commiting all at once.

like image 65
Mchl Avatar answered Sep 27 '22 17:09

Mchl