Transaction isolation levels are the least understood feature of relational databases. Most developers don’t know them and just use the default one. Actually, a relevant part of them even believe they use MySQL without transactions.
Amongst isolation levels,
READ UNCOMMITTED is the least understood. So here’s a quick note about what it is and why – if you know about it – you probably have false beliefs.
READ UNCOMMITTED is totally inconsistent. It sees changes (new, deleted, modified rows) made by other transactions, that didn’t
COMMIT yet. And actually, it’s possible that those transactions will fail, which leads
READ UNCOMMITTED to see something that will never happen.
Despite this, it is extremely useful in some cases. For example:
- To run
SELECTs which read a huge amount of rows for analytics.
DELETErows not used by anyone (archived historical contents).
INSERTrows when no one else can possibly insert new rows.
When using the default isolation level, or even
READ COMMITTED, if your transaction involves many rows (say, millions) your statements will probably be slow, and will probably use much CPU time, maybe will also cause replication lag.
READ UNCOMMITTED is not magic and cannot make a slow query fast. But it can save a lot of transaction logs processing, making a query a bit faster and avoiding consuming many of resources for no reason.
That said, its drawbacks should also be noted:
- Bugs. Precisely because it is the least used (tested) isolation level.
- It would be a mistake to assume that
READ UNCOMMITTEDis lock-free. When trying to modify a locked row, it’s queued. When writing anything, it puts a lock. In some cases (like the ones listed above) this is irrelevant, but you need to understand this.
- Selecting all rows from a table while it’s been intensively INSERTed is slower with
One last important detail. When I explain isolation levels, people tend to think that
READ UNCOMMITTED doesn’t take snapshots. This is wrong: it takes a snapshots for every statement it runs, but snapshots include uncommitted changes.
EDIT: Actually there is no snapshot, see the comments. But despite my mistake, the following part remains true.
This may sound slightly obscure. To understand better, create a test table and run something like:
SELECT a FROM my_test WHERE a > SLEEP(a);
The query will evaluate each row, and the evaluation will cause a wait of some seconds. So you have the time to add a new row from another connection. The row will not appear in the results of these query – it’s not in the snapshot that has been taken when the query was issued. However, if you run the query again, the new row will be there, because a new snapshot is taken.
Links for the curious:
- MySQL isolation levels
- PostgreSQL isolation levels (spoiler:
READ UNCOMMITTEDis a synonym for