Why better isolation level means better performance in SQL Server
When measuring performance on my query I came up with a dependency between isolation level and elapsed time that was surprising to me
READUNCOMMITTED - 409024
READCOMMITTED - 368021
REPEATABLEREAD - 358019
SERIALIZABLE - 348019
Left 开发者_运维百科column is table hint, and the right column is elapsed time in microseconds (sys.dm_exec_query_stats.total_elapsed_time). Why better isolation level gives better performance? This is a development machine and no concurrency whatsoever happens. I would expect READUNCOMMITTED to be the fasted due to less locking overhead.
Update: I did measure this with
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
issued and Profiler confirms there're no cache hits happening.
First of all, you need to run the query repeatedly under each isolation level and average the result, discarding the one with the maximum time. This will eliminate the buffer warm up impact: you want all runs to be on a warm cache, not have one query warm the cache and pay the penalty in comparison.
Next, you need to make sure you measure under realistic concurrency scenario. IF you will have updates/inserts/deletes occur under real life, then you must add them to your test, since they will impact tremendously the reads under various isolation level. The last thing you want is to conclude 'serializable reads are fastest, lets use them everywhere' and then watch the system melt down in production because everything is serialized.
Other than that, the only isolation level that is legitimately faster is dirty reads, since it doesn't acquire locks. Read committed snapshot (which you did not measure) also doesn't acquire locks, but it does impact performance overall due to row versioning overhead.
Now that I understand isolation levels better I see that better isolation level could allow some smart optimizations. For example, once transaction reads some data isolation level might stipulate it is ought to use that data till the end rather than try to re-read them from disk.
I still would be interested to read some in-depth look on this.
精彩评论