Isolation Level

  • I am clearing my funda regarding Isolation level (ACID), but I am confused with three word

    1. Dirty Reads,

    2. Phantom Reads,

    3. Non-Repeatable Reads.

    I googled for this three terms and seen some exapmle but not sure how they are differs. All are looking same:( .

    Can anyone please differentiate them so I can understand Isolation.

    Thanks in advance..!

  • Check this: http://msdn.microsoft.com/en-us/library/aa213029(v=SQL.80).aspx

    See if that helps.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Dirty reads (possible only in read uncommitted isolation level):

    Read data that is subsequently rolled back and was never part of the durable contents of the database. Eg

    Begin transaction

    update Employees set status = 'fired'

    rollback;

    If you run a select against the employees table while that is running and you use read uncommitted, you will see lots and lots of people fired, but those changes were rolled back.

    Non-repeatable reads (possible in read uncommitted and read committed isolation levels)

    Run a select twice within a transaction, get different results because some other process modified the row between when the first select ran and the second ran.

    Phantom reads (possible in read uncommitted, read committed isolation levels and repeatable read isolation levels)

    Run a select twice within a transaction, get more rows the second time (rows that were there can't change though). This is because another process inserted a row between when the first select ran and the second.

    See chapter 6 of http://www.simple-talk.com/books/sql-books/troubleshooting-sql-server-a-guide-for-the-accidental-dba/. iirc there's a good coverage of locks and isolation levels.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks a lot ! Given link is very helpful to understand those three terms with an example.

    http://msdn.microsoft.com/en-us/library/aa213029(v=SQL.80).aspx

    but I am not cleared about Phantom Reads. It looks same like non-repeatable reads.

    If I read one transaction, after that some one will modified transaction. And when I read that same transaction again, the result will be different from my first read thats non-repeatable read says and so phantom read too.

    Can you have specific difference so I can differentiate them ?

  • Unrepeatable example: Update Joe's last name from Smith to Smithe between reads. Different data, same rows.

    Phantom Rows: 5 rows one time, 4 or 6 the next time.

    One set of mixed up reads can actually do both things if both insert/delete and update actions are occuring between reads.

    Edit to clarify.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (4/25/2012)


    Unrepeatable example: Update Joe's last name from Smith to Smithe between reads. Different data, same rows.

    Phantom Rows: 5 rows one time, 4 or 6 the next time.

    6 or more. Can't be 4. Phantom rows are new rows that appear in the set. Rows that disappear are non-repeatable reads.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • jitendra.padhiyar (4/25/2012)


    If I read one transaction, after that some one will modified transaction. And when I read that same transaction again, the result will be different from my first read thats non-repeatable read says and so phantom read too.

    Non-repeatable read: Rows that you read last time either have disappeared or have changed values

    Phantom read: New rows have appeared in the resultset. Rows that were not there the first time.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (4/25/2012)


    GSquared (4/25/2012)


    Unrepeatable example: Update Joe's last name from Smith to Smithe between reads. Different data, same rows.

    Phantom Rows: 5 rows one time, 4 or 6 the next time.

    6 or more. Can't be 4. Phantom rows are new rows that appear in the set. Rows that disappear are non-repeatable reads.

    Per MSDN (emphasis added):

    Phantom Reads

    Phantom reads occur when an insert or delete action is performed against a row that belongs to a range of rows being read by a transaction. The transaction's first read of the range of rows shows a row that no longer exists in the second or succeeding read, as a result of a deletion by a different transaction. Similarly, as the result of an insert by a different transaction, the transaction's second or succeeding read shows a row that did not exist in the original read.

    For example, an editor makes changes to a document submitted by a writer, but when the changes are incorporated into the master copy of the document by the production department, they find that new unedited material has been added to the document by the author. This problem could be avoided if no one could add new material to the document until the editor and production department finish working with the original document.

    Per Oracle documentation:

    A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first.

    Either definition allows for the phantoms to be either in the first query but not the second (deletes) or in the second but not the first (inserts).

    Do you have a reference that refutes this?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • http://en.wikipedia.org/wiki/Isolation_%28database_systems%29

    Phantom reads

    A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first.

    This can occur when range locks are not acquired on performing a SELECT ... WHERE operation. The phantom reads anomaly is a special case of Non-repeatable reads when Transaction 1 repeats a ranged SELECT ... WHERE query and, in the middle of both operations, Transaction 2 creates (i.e. INSERT) new rows (in the target table) which fulfill that WHERE clause.

    MSDN http://msdn.microsoft.com/en-us/library/ms190805.aspx

    A phantom read is a situation that occurs when two idential queries are executed and the collection of rows returned by the second query is different. The example below shows how this may occur. Assume the two transactions below are executing at the same time. The two SELECT statements in the first transaction may return different results because the INSERT statement in the second transaction changes the data used by both.

    http://social.msdn.microsoft.com/Forums/en/sqldocumentation/thread/f4924482-4ce3-4e25-86fd-126c7e97a33b

    The reference to 'delete' is incorrect and will be corrected in a future version

    I can also prove it.

    The repeatable read isolation level allows only phantom rows, not non-repeatable reads?

    Table creation:

    CREATE TABLE Test (

    id INT

    )

    INSERT INTO Test (id)

    SELECT TOP(50) ROW_NUMBER() OVER (ORDER BY (SELECT (1))) * 2

    FROM sys.objects AS o

    Window 1:

    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

    BEGIN TRAN

    SELECT * FROM test WITH (ROWLOCK) WHERE id BETWEEN 10 AND 20

    WAITFOR DELAY '00:00:30'

    SELECT * FROM test WITH (ROWLOCK) WHERE id BETWEEN 10 AND 20

    Window 2:

    DELETE FROM dbo.Test WHERE id = 14

    Result: delete blocks, both selects return 6 rows.

    Repeat test, this time with window 2 as

    INSERT INTO dbo.Test (id) VALUES (15)

    Result: Insert completes immediately. In the select window, first resultset has 6 rows, second has 7.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • To explain, in repeatable read isolation level, shared locks are held until the transaction commits or rolls back. So when the delete comes along and tries to delete a row that was read by the first select, it can't, there's a shared lock on that row until the transaction commits and the delete cannot get the X lock it requires until the transaction with the two selects has committed or rolled back.

    Inserts are allowed because only serialisable takes key range locks.

    A delete causes a non-repeatable read, which can only happen in read uncommitted and read committed.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • And, of course, having first read it in about 2000, I never bothered to update my definition.

    Thanks.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks a lot GilaMonster and GSquare to provide me good descreption. I read each post and at last i understood as below:

    Non-Repeatable reads: Rows that disappear are non-repeatable reads.

    That means same rows but data will be chagned.

    Phantom Reads: Phantom rows are new rows that appear in the set.

    That means you will get new row set along with previous row set. Suppose on my first read i had 4 rows with different data. on my second read I will have more that 4 rows with different values.

    Am I correct?

  • jitendra.padhiyar (4/25/2012)


    Non-Repeatable reads: Rows that disappear are non-repeatable reads.

    That means same rows but data will be chagned.

    Deleted or updated. So you could run the same query twice, get this as the first result

    Brian White employed

    Dan Black fired

    Robert Green employed

    and this as the second

    Brian White resigned

    Robert Green employed

    One row updated, one row deleted.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • For my own understanding... We are talking about select1 and select2 being in the same transaction. Am I wrong? I would assume different results otherwise in a highly active OLTP database.

    Jared
    CE - Microsoft

  • SQLKnowItAll (4/25/2012)


    For my own understanding... We are talking about select1 and select2 being in the same transaction. Am I wrong? I would assume different results otherwise in a highly active OLTP database.

    You're right. Isolation levels apply to actions within a transaction. Separate transactions with different results is expected and usual.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply