Tuesday, October 9, 2012

Programmer competency

This link here is nice tool to gauge self or whatever.

http://www.starling-software.com/employment/programmer-competency-matrix.html

Isolation Levels in SQL Server

Isolation is the database property that keeps concurrent transactions independent. There are multiple levels of isolation provided in SQL Server 2008.

1. Read Uncommitted
2. Read Committed
3. Repeatable Read
4. Serializable
5. Snapshot

We need to know two terms before we learn the details of the isolation levels.

Dirty Read is when you read uncommitted data. The select statement returns rows which are being processed by an unfinished transaction.

Phantom Read means that the the data that has been read may have been changed by another transaction since the data was last read. Another read may result in a different data being returned.

Read Uncommitted
This is the lowest isolation level that exists. This isolation level allows you to read data that is still being modified by some other unfinished transaction. It allows transactions to occur on the data even when you are reading it.

As expected, this Read Uncommitted may give out dirty reads without waiting for ongoing transactions to complete.So the transaction does not lock the data for read or reads do not lock the data for transaction.

Read Committed
In SQL Server, this is the default isolation level. It returns only the committed data. The select query (read) will wait till the any other transaction has exclusive lock on that data.

Repeatable Read 
This level adds additional guarantee to the read committed isolation level that multiple reads within the transaction will give out the same data.

This isolation level will place exclusive lock on the data that is being read in a transaction. So multiple reads within the transaction will return the same data even if other transactions are trying for read locks(shared) for the same data. Other read transactions will wait till the transaction with repeatable read isolation level completes.

Serializable
This is one step beyond repeatable read in that it ensures that no data is added (or modified) during a transaction with serializable isolation level. The query that modifies the data has to wait until the transaction with serializable level has completed. So this isolation level actually eliminates the possibility of phantom read.

Snapshot
This is the exact same thing as serializable. However this does not block any query inserting or updating. Instead, it creates a snapshot of data being read at that time for that transaction. If the data is read again in that transaction, it reads from the snapshot specific to that transaction. So you will get the same data within the transaction even if some other concurrent transaction has changed that data.

So no transactions are blocked for other transactions to run. However, on the negative side, this isolation level needs additional server resources especially when the data being handled is huge.

You can run DBCC useroptions command to get the details like date format and isolation level.

DBCC useroptions
Set Option                  Value
--------------------------- --------------
textsize                    2147483647
language                    us_english
dateformat                  mdy
datefirst                   7
lock_timeout                -1
quoted_identifier           SET
arithabort                  SET
ansi_null_dflt_on           SET
ansi_warnings               SET
ansi_padding                SET
ansi_nulls                  SET
concat_null_yields_null     SET
isolation level             read committed
Conclusion is that the higher you go in the isolation level, the lesser is the concurrency. But, the performance may be adversely affected because of additional blocking. One should use only the level that is required and stay clear of higher isolation levels if not really needed.