【Weekly SQLpassion Newsletter】The unwanted Side Effects of enabling Read Committed Snapshot Isolation

A lot of developers are approaching me very often, and tell me that they are using the NOLOCK query hint to get rid of blocking situations in SQL Server. Unfortunately the NOLOCK query hint is not really a good solution when you hit blocking situations, because SQL Server can return you uncommitted data.

Therefore I recommend very often to use Optimistic Concurrency – especially Read Committed Snapshot Isolation (RCSI) – because it’s a simple database configuration change. But in today’s blog posting I don’t want to go into the details of Read Committed Snapshot Isolation. Today I want to show you some unwanted side effects that you can introduce in your database when you enable Read Committed Snapshot Isolation.

What is Read Committed Snapshot Isolation?

The idea behind Read Committed Snapshot Isolation is the following: instead of locking a record during the reading phase with a Shared Lock, SQL Server will transparently return you the old committed record version from the Version Store. The Version Store is stored in TempDb. Therefore it is also very important that you have carefully designed and planned TempDb. But that’s a topic for another blog posting…

As soon as you are using Read Committed Snapshot Isolation your current record version points with 14 additional bytes to an older record version which is stored in TempDb. And this 14 additional bytes, which are used for the pointer into TempDb, can introduce now some serious problems. Why? Let’s have a look at it.

The unwanted Side Effects of enabling Read Committed Snapshot Isolation

(Be sure to checkout the FREE SQLpassion Performance Tuning Training Plan - you get a weekly email packed with all the essential knowledge you need to know about performance tuning on SQL Server.)

 

A lot of developers are approaching me very often, and tell me that they are using the NOLOCK query hint to get rid of blocking situations in SQL Server. Unfortunately the NOLOCK query hint is not really a good solution when you hit blocking situations, because SQL Server can return you uncommitted data.

Therefore I recommend very often to use Optimistic Concurrency – especially Read Committed Snapshot Isolation (RCSI) – because it’s a simple database configuration change. But in today’s blog posting I don’t want to go into the details of Read Committed Snapshot Isolation. Today I want to show you some unwanted side effects that you can introduce in your database when you enable Read Committed Snapshot Isolation.

What is Read Committed Snapshot Isolation?

The idea behind Read Committed Snapshot Isolation is the following: instead of locking a record during the reading phase with a Shared Lock, SQL Server will transparently return you the old committed record version from the Version Store. The Version Store is stored in TempDb. Therefore it is also very important that you have carefully designed and planned TempDb. But that’s a topic for another blog posting…

As soon as you are using Read Committed Snapshot Isolation your current record version points with 14 additional bytes to an older record version which is stored in TempDb. And this 14 additional bytes, which are used for the pointer into TempDb, can introduce now some serious problems. Why? Let’s have a look at it.

14 bytes matter!

Imagine you have an already created database with some tables. To demonstrate that scenario, let’s create a simple table and insert some records into it.

-- Let's create a new table without RCSI enabled
CREATE TABLE Test
(
    Col1 INT IDENTITY(1, 1) PRIMARY KEY NOT NULL,
    Col2 CHAR(1000),
    Col3 CHAR(1000)
)
GO

-- Let's insert 100 records into the table
INSERT INTO Test VALUES (REPLICATE('a', 1000), REPLICATE('b', 1000))
GO 100

As you can see from the table definition, the record size is 2011 bytes long (7 bytes overhead + 4 bytes integer + 2x 1000 bytes character). You can also verify the record length by using the DBCC PAGE command in combination with a Data Page ID.

-- Let's dump out a specific Data Page.
-- The record size is currently 2011 bytes (7 + 4 + 1000 + 1000).
DBCC PAGE(RCSI_SideEffects, 1, 224, 1)
GO

The record length is 2011 bytes

And now let’s enable Read Committed Snapshot Isolation. As I’m always saying it’s a simple database configuration change. It’s so easy!

posted @ 2018-09-03 11:20  FH1004322  阅读(195)  评论(0)    收藏  举报