Into The Yonder
When you create a database in SQL Server (everything except Azure SQL DB), you get this garbage isolation level called Read Committed.
That isolation level, along with others like Repeatable Read and Serializable, are considered pessimistic. Though Repeatable Read and Serializable are less garbage, it comes with a strictness of locking that most applications don’t need across the board. They may need it for certain niche activities, but you know…
Then there are isolation levels that are quite useful for most isolation levels, and they’re called optimistic isolation levels. In SQL Server, they’re Snapshot Isolation (SI), and Read Committed Snapshot Isolation (RCSI).
I think they are very much not-garbage, and so do other major database platforms that use specific implementations of MVCC (Multi Version Concurrency Control) by default. There may be some historical reason for SQL Server not doing it by default, which is also garbage.
There are some differences between the two optimistic isolation levels, which makes them useful in different situations. Most people don’t need both turned on, which is something I see quite a bit, but there just might be someone out there who turns on and actually uses both.
To generalize a little bit:
- SI is good when you only want certain queries to read versioned data
- RCSI is good when you want every query to read versioned data
What’s versioned data? You can think of it like the “last known good” version of a row before a modification started.
When an update or a delete starts to change data, SQL Server will send those last known good versions up to tempdb for read queries to grab what they need rather than getting blocked. Inserts are a little different, because they are the only known good version of a row.
There are some other differences, too.
- Can be turned on without exclusive access to the database
- Queries all read data as it looked at the beginning of a transaction
- Does need exclusive access to the database, but it’s not as bad as it sounds
- Reads data as it looked when each query in a transaction starts
Getting exclusive access to the database can be done without the single-user/multi-user dance:
ALTER DATABASE YourDatabase SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
If You Know You Know
The reasons why you might want to turn these on are when your application performance suffers because of locking or deadlocking.
If you want some quick and dirty queries to figure out if you’ve got those happening, you can run these queries.
/*Lock waits?*/ SELECT dows.wait_type, dows.waiting_tasks_count, dows.wait_time_ms, dows.max_wait_time_ms, dows.signal_wait_time_ms FROM sys.dm_os_wait_stats AS dows WHERE dows.wait_type LIKE 'LCK%' AND dows.waiting_tasks_count > 0 ORDER BY dows.wait_time_ms DESC; /*Deadlocks?*/ SELECT p.object_name, p.counter_name, p.cntr_value FROM sys.dm_os_performance_counters p WHERE TRIM(p.counter_name) = 'Number of Deadlocks/sec' AND TRIM(p.instance_name) = '_Total';
What you want to look for in general are when readers and writers are interfering with each other. If your blocking or deadlocking problems are between any kind of exclusive locks, optimistic isolation levels won’t help you.
Wait stats from readers will generally have an “S” in them, like LCK_M_S. The same goes for deadlocks, where the lock mode will have an S in either the owner or the waiter.
Reader Writer Fighter
It’s important to keep in mind that it’s not just writers that block readers, or writers that can deadlock.
This is where the “Shared” lock and lock mode stuff comes into play. Again, if all your locks and deadlocks are between modification queries — locks and lock modes with X (exclusive) or U (update) — they’ll still block each other.
There’s a lot more details at the linked posts above, but that’s the general pattern. Another pattern to look for is if your developers keep adding more and more NOLOCK hints to “fix performance issues”.
A lot of times they’re just covering up other issues with indexing or the way queries are written, or they’re totally misunderstood. I’ve said it before, but it doesn’t mean your query doesn’t take any locks, it means that your query doesn’t respect locks taken by other queries.
That often comes as a surprise to people when I tell them, so I say it whenever I write about it. But that’s where the bad reputation comes from — it can read all sorts of in-flight data that may not reflect reality.
Thanks for reading!
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. I’m offering a 75% discount to my blog readers if you click from here. I’m also available for consulting if you just don’t have time for that and need to solve performance problems quickly.