MySQL Performance Implications of InnoDB Isolation Modes
Join the DZone community and get the full member experience.Join For Free
over the past few months i’ve written a couple of posts about dangerous debt of innodb transactional history and about the fact mvcc can be the cause of severe mysql performance issues . in this post i will cover a related topic – innodb transaction isolation modes, their relationship with mvcc (multi-version concurrency control) and how they impact mysql performance.
the mysql manual provides a decent description of transaction isolation modes supported by mysql – i will not repeat it here but rather focus on performance implications.
serializable – this is the strongest isolation mode to the point it essentially defeats multi-versioning making all selects locking causing significant overhead both in terms of lock management (setting locks is expensive) and the concurrency you can get. this mode is only used in very special circumstances among mysql applications.
repeatable read – this is default isolation level and generally it is quite nice and convenient for the application. it sees all the data at the time of the first read (assuming using standard non-locking reads). this however comes at high cost – innodb needs to maintain transaction history up to the point of transaction start, which can be very expensive. the worse-case scenario being applications with a high level of updates and hot rows – you really do not want innodb to deal with rows which have hundreds of thousands of versions.
in terms of performance impact, both reads and writes can be impacted. for select queries traversing multiple row versions is very expensive but so it is also for updates, especially as version control seems to cause severe contention issues in mysql 5.6
here is example: i ran sysbench for a completely in-memory data set and when start transaction and run full table scan query couple of times while keeping transaction open:
sysbench --num-threads=64 --report-interval=10 --max-time=0 --max-requests=0 --rand-type=pareto --oltp-table-size=80000000 --mysql-user=root --mysql-password= --mysql-db=sbinnodb --test=/usr/share/doc/sysbench/tests/db/update_index.lua run
as you can see the write throughput drops drastically and stays low at all times when transaction is open, and not only when the query is running. this is perhaps the worse-case scenario i could find which happens when you have select outside of transaction followed by a long transaction in repeatable-read isolation mode. though you can see regression in other cases, too.
here is the set of queries i used if someone wants to repeat the test:
select avg(length(c)) from sbtest1; begin; select avg(length(c)) from sbtest1; select sleep(300); commit;
not only is repeatable read the default isolation level, it is also used for logical backups with innodb – think
these results show such backup methods not only can’t be used with large data sets due to long recovery time but also can’t be used with some high write environments due to their performance impact.
read committed mode is similar to repeatable read with the essential difference being what versions are not kept to the start of first read in transaction, but instead to the start of the current statement. as such using this mode allows innodb to maintain a lot less versions, especially if you do not have very long running statements . if you have some long running selects – such as reporting queries performance impact can be still severe.
in general i think good practice is to use read comitted isolation mode as default and change to repeatable read for those applications or transactions which require it.
read uncommitted – i think this is the least understood isolation mode (not a surprise as it only has 2 lines of documentation about it) which only describe it from the logical point of view. if you’re using this isolation mode you will see all the changes done in the database as they happen, even those by transactions which have not been yet committed. one nice use case for this isolation mode is what you can “watch” as some large scale update statements are happening with dirty reads showing which rows have already been changes and which have not.
so this statement shows changes that have not been committed yet and might not ever be committed if the transaction doing them runs into some errors so – this mode should be used with extreme care. there are a number of cases though when we do not need 100% accurate data and in this case this mode becomes very handy.
so how does read uncommitted behave from a performance point of view? in theory innodb could purge row versions even if they have been created after the start of the statement in read uncommitted mode. in practice, due to a bug or some intricate implementation detail it does not do that – row versions still will be kept to the start of the statement. so if you run very long running select in read uncommitted statements you will get a large amount of row versions created, just as if you would use read committed. no win here.
there is an important win from select side – read uncommitted isolation mode means innodb never needs to go and examine the older row versions – the last row version is always the correct one which can cause dramatic performance improvement especially if the undo space had spilled over to the disk so finding old row versions can cause a lot of io.
perhaps the best illustration i discovered with query select avg(k) from sbtest1; ran in parallel with the same update heavy workload stated above. in read committed isolation mode it never completes – i assume because new index entries are inserted faster than they are scanned, in case of read uncommitted isolation mode it completes in a minute or so.
final thoughts: using innodb isolation modes correctly can help your application to get the best possible performance. your mileage may vary and in some cases you will see no difference; in others it will be absolutely dramatic. there also seems to be a lot of work to be done in relationship to innodb performance with long version history. i hope it will be tackled in the future mysql version.
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.