In MySQL 5.6, using InnoDB, I have a rather large table, call it A; (about 4 million rows) in which I need to update a field. The condition regarding what to update is held in another table, B; and the relation between the two is also held in another table, C; so I created my update statement with two joins, like so :
UPDATE A a JOIN (B b, C c) ON (conditions) SET a.myField = 'myValue'
Now I tested this statement on a dump of my target database, on a equivalent machine. Because I needed this dump for other purposes, I tested the thing using a transaction in order to rollback afterwards :
START TRANSACTION;
UPDATE [...]
ROLLBACK;
Which gave me pretty good performance (I think) for my update, about 17 seconds and 8 for the rollback.
My question is this: If I were to run this statement as is, not in a transaction, on my target database, would I obtain the same performance ? In other words, does using explicit transactions affect performance, and if yes, how ?
I tried to look the documentation on this but it is rather sparse on this topic.
Best Answer
1: Using autocommit to automatically commit each transaction:
2: Explicitly turning a single statement into a transaction. This is 'identical' to #1.
3: Multiple statements in a transaction:
4: Don't use this; some day you will forget to
COMMIT
:5: Not advised. It can lead to slowdowns due to overflowing the "log":
Notes:
innodb_flush_log_at_trx_commit
for a tradeoff between integrity and speed.Advice:
INSERT
, it is even better to have a singleINSERT
statement with multiple rows. Davit's Comment show a 9x speedup by putting multipleINSERTs
in a single transaction. That is similar to the effect of the single batch insert.ROLLBACK
only when appropriate. It is costly. This is because InnoDB optimizes forCOMMIT
and has to do extra effort to undo.DELETE
andUPDATE
have a lot of overhead and stress on the log. It is better to break them up.