Showing posts with label Interview Question and Answers. Show all posts
Showing posts with label Interview Question and Answers. Show all posts

Sunday, August 9, 2020

Snapshot Isolation Level in SQL Server with example

Snapshot

Snapshot isolation is similar to Serializable isolation. The difference is Snapshot does not hold lock on table during the transaction so table can be modified in other sessions. Snapshot isolation maintains versioning in Tempdb for old data in case of any data modification occurs in other sessions then existing transaction displays the old data from Tempdb.

 IF OBJECT_ID('Product') is not null

    begin

    DROP TABLE Product

    end

create table Product

(ProdID int,

ProdName Varchar(50),

ProdPrice int

)

 insert into Product(ProdID,ProdName,ProdPrice)

values

( 1,'Black Shirt',1000),

( 2,'White Suits',2000),

( 3,'Red Kurta',700),

( 4,'Half Shirt',1700)

Run both the sessions side by side

Example 1:

 Session 1:

set transaction isolation level snapshot

begin tran

select * from Product

waitfor delay '00:00:10'

select * from Product

rollback

 Session 2:

 insert into Product(ProdID,ProdName,ProdPrice)

values ( 8,'Red Pant',1500)

Output from session 1:

Session 2 will execute without any delay as Session 1 will not lock the table.

Click on Below links to see the Different types of isolation levels with example


Serializable Isolation Level in SQL Server with example

Serializable 

Serializable Isolation is similar to Repeatable Read Isolation but the difference is it prevents Phantom Read. This works based on range lock. If table has index then it locks records based on index range used in WHERE clause. If table doesn't have index then it locks complete table.

 IF OBJECT_ID('Product') is not null

    begin

    DROP TABLE Product

    end

create table Product

(ProdID int,

ProdName Varchar(50),

ProdPrice int

)

 insert into Product(ProdID,ProdName,ProdPrice)

values

( 1,'Black Shirt',1000),

( 2,'White Suits',2000),

( 3,'Red Kurta',700),

( 4,'Half Shirt',1700)

Run both the sessions side by side

 Example 1:

Product table does not have any index. SO it will lock whole records

 Session 1:

set transaction isolation level serializable

begin tran

select * from Product

waitfor delay '00:00:10'

select * from Product

rollback

 Session 2:

 insert into Product(ProdID,ProdName,ProdPrice)

values ( 6,'Blue Pant',1400)

 Output from Session 1:


Since table does not have any index session 1 lock whole table. So session 2 waited till session 1 completed transaction. So it prevents from phantom read.

 Example 2:

 Product table is having index on ProdID. SO it will lock only selected range of records

Session 1:

set transaction isolation level serializable

begin tran

select * from Product where ProdID in(1,4)

waitfor delay '00:00:10'

select * from Product where ProdID in(1,4)

rollback

 Session 2:

 insert into Product(ProdID,ProdName,ProdPrice)

values ( 7,'Grey Kurta',1200)

 Now session 2 will execute without any delay. Since session locked the records from ID 1 to 4.



Click on Below links to see the Different types of isolation levels with example

Repeatable Read Isolation Level in SQL Server with example

Repeatable Read

In Repeatable Read, statements cannot read data that has been modified but not yet committed by other transactions. No other transaction can modify data that has been read by the current transaction until the current transaction completes.

Shared locks are placed on all data read by each statement in the transaction and are held until the transaction completes. This prevents other transactions from modifying any rows that have been read by the current transaction. This isolation level prevents the Non Repeatable Read issue.

 IF OBJECT_ID('Product') is not null

    begin

    DROP TABLE Product

    end

create table Product

(ProdID int,

ProdName Varchar(50),

ProdPrice int

)

 insert into Product(ProdID,ProdName,ProdPrice)

values

( 1,'Black Shirt',1000),

( 2,'White Suits',2000),

( 3,'Red Kurta',700),

( 4,'Half Shirt',1700)

  

Open two sessions

Run both the sessions side by side 

Example 1:

Session 1:

set transaction isolation level repeatable read

begin tran

select * from Product where ProdID in(1,2)

waitfor delay '00:00:15'

select * from Product where ProdID in (1,2)

rollback

Session 2:

update Product set ProdPrice=999 where ProdID=1

Output Session 1:

Update command in session 2 will wait till session 1 transaction is completed because ProdID = 1 has been used in session 1.

Example 2:

Session 1:

set transaction isolation level repeatable read

begin tran

select * from Product

waitfor delay '00:00:15'

select * from Product

rollback

Session 2:

insert into Product(ProdID,ProdName,ProdPrice)

values ( 5,'Blue Jacket',4000)

 

Output Session 1:

session 2 will execute without any delay because it has insert query for new entry. This isolation level allows to insert new data but does not allow to modify data.

Now you can see a new record inserted.

 Example 3:

 Session 1:

set transaction isolation level repeatable read

begin tran

select * from Product where ProdID in(1,2)

waitfor delay '00:00:15'

select * from Product where ProdID in (1,2)

rollback

Session 2:

update Product set ProdPrice=999 where ProdID=3

Session 2 will execute without any delay because ID 1 and 2 are locked in session 1.


Click on Below links to see the Different types of isolation levels with example