Dirty Reads

  • plz let me know small example

  • Do not use WITH NOLOCK on rows and tables.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • 2 ways:

    /* 1st */

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    SELECT * FROM MyTable

    /* 2nd */

    SELECT * FROM MyTable WITH (NOLOCK)

    I hope this is what you're looking for.

  • Something along these lines. You have a table with a column, CityName. One row has a value 'Hyderabad.' But that's a bad value. So someone is running a query that looks like this:

    BEGIN TRANSACTION

    UPDATE MyTable

    SET CityName = 'Puducherry'

    WHERE CityName = 'Hyderabad'

    This is beginning a transaction and updating the value, but right now, it's not committed. Someone else runs a query like this:

    SELECT CityName

    FROM MyTable WITH (NOLOCK)

    What happens is, they get a "dirty" read, they will see the value 'Hyderabad' even though there is a transaction occurring that will make that value into 'Puducherry.' That's a very simple example of a dirty read. It's actually much more complicated than that. A dirty read can also cause you to get duplicate rows, extra rows, or miss rows of data, not just get that uncommitted column value. Dirty reads are quite dangerous if you're working on a system that needs real data integrity for the business (and most do).

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Henrico Bekker (7/22/2010)


    Do not use row and table locks.

    How would not using locks prevent dirty reads? You suggesting page locks only?

    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 (7/22/2010)


    Henrico Bekker (7/22/2010)


    Do not use row and table locks.

    How would not using locks prevent dirty reads? You suggesting page locks only?

    tsk...I meant WITH NOLOCK...my bad.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • how can i prevent the dirty reads

  • At least use

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED

    in your transactions. Total guarantee of data accuracy (though it will cause issues with concurrency if the transaction is too long) would be

    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

  • Grant Fritchey (7/22/2010)


    Something along these lines. You have a table with a column, CityName. One row has a value 'Hyderabad.' But that's a bad value. So someone is running a query that looks like this:

    BEGIN TRANSACTION

    UPDATE MyTable

    SET CityName = 'Puducherry'

    WHERE CityName = 'Hyderabad'

    This is beginning a transaction and updating the value, but right now, it's not committed. Someone else runs a query like this:

    SELECT CityName

    FROM MyTable WITH (NOLOCK)

    What happens is, they get a "dirty" read, they will see the value 'Hyderabad' even though there is a transaction occurring that will make that value into 'Puducherry.' That's a very simple example of a dirty read. It's actually much more complicated than that. A dirty read can also cause you to get duplicate rows, extra rows, or miss rows of data, not just get that uncommitted column value. Dirty reads are quite dangerous if you're working on a system that needs real data integrity for the business (and most do).

    This explanation nails the question on this post. Read Grant's book. It has a chapter on Transactions and Locking. It's a book we DBAs have been waiting for.

  • Do not use row and table locks.

  • Do not use row and table locks.

  • sreedhar1.m (7/23/2010)


    Do not use row and table locks.

    A couple of questions. How do you go about not using locks? Aren't they pretty much required by the system when it updates data? Is it possible that you meant don't use lock hints?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey (7/23/2010)


    sreedhar1.m (7/23/2010)


    Do not use row and table locks.

    A couple of questions. How do you go about not using locks? Aren't they pretty much required by the system when it updates data? Is it possible that you meant don't use lock hints?

    I think he's just repeating what Henrico said earlier in this thread.

    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

Viewing 13 posts - 1 through 12 (of 12 total)

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