Note: This is another great post from a PHP community member. This post talks about MySQL and InnoDB, written by the awesome Morgan Tocker, MySQL Community Manager at Oracle. Check out more about Morgan here.
Here is a question I’ve actually been asked a few times:
“I am writing a batch processing script which modifies data as part of an ongoing process that is scheduled by cron. I have the ability to group a number of modifications together into a transaction, but I’m not sure what the correct number is?”
First off, I think that this question is interesting not just in the context of batch processing, but it equally applies to all parts of the application. If you are designing a high throughput system for MySQL, there are actually some potential pain points that you can design your way around.
Potential Pain Points
Here are the situations where the size of the transaction could impact performance:
Very Small Transactions
auto_commit mode (or modifying just one row at a time) can cause some internal pressure on InnoDB’s redo log system, since it has to make sure that its in-memory buffer is safely flushed to disk. This flushing is required to be able to provide durability, and safely recover all changes that have been committed if the server crashes.
Note that MySQL does offer configuration to relax the flushing behavior, but it comes at the cost of ACID Compliance. Which is to say that you may lose data in the event of a failure.
Very Long Transactions
To understand what I mean by “long”, lets use this hypothetical example:
START TRANSACTION; /* Modify Data */ sleep(60) COMMIT;
The problem with these transactions, is that even if they do not modify much data, because of a feature called Multiversion Concurrency Control InnoDB internally needs to keep around older versions of rows that were modified in other transactions. There is an internal clean-up thread which is constantly trying to purge older versions, but in the case of a very long transaction it will sit and wait until after a
ROLLBACK event occurs.
In the example above, I used
sleep(60) to illustrate a point, but in a very high throughut system it may not even need to be this long to start impacting performance.
Very large Transactions
By very large transactions, I simply mean transactions which modify very large amounts of data. There are a couple of risks that you need to be aware of in this case:
- Locks will be held for the duration of a transaction. By modifying more rows at once, you may increase the number of deadlocks or introduce lock wait contention.
- Rolling back transactions can be 30 times the cost of a corresponding apply transaction. It is important to note that rolling back may be something you requested, but it will also happen in the case of a disconnect or a deadlock.
I think it’s easy to get lost in the details and sub-optimize to eliminate any potential problems. Fear not! I have a two step process to avoiding the vast majority of all issues:
Move intensive application processing logic to before or after transaction running. While you have a transaction open, try and make any changes that you need to make and
COMMITas soon as possible. i.e.
/* do intensive processing / START TRANSACTION; / Modify Data / COMMIT; / more intensive processing */
You usually can’t go wrong by using transactions as they were originally intended: commit logical units of work together. As mentioned above you can actually run into problems using
auto_commitwith row-at-a-time processing, as well as batches that are too large. My advice would be to keep transactions to modifying less than 10K rows at a time.