with nolock in sql server please advice

  • I have read lots of article about with nolock, and have seen many projects using with nolock option.

    Please advice me if i am right.

    I have read lots of article about with nolock, and have seen many projects using with nolock option.

    Please advice me if i am right.

    1.Normal select queries can use with no lock

    2. Batch jobs should not use with no lock.

    please guide me, am i right.

    Thanks

  • Here is my input.

    I do not think we have a clear cut on your questions.

    Generally speaking, when there are many users and objects are huge, we may need to use NO_LOCK. Our decision is made based on our the above factors and the torlerance of our users.

    NO_LOCK may cause insistencies if we abuse it, however.

  • No lock's not something you should be using all the time. It's for when you have concurrency issues and you don't mind the chance of inaccurate data.

    Selects can be run with no lock, however all forms of data modification will take locks, even if you specify the nolock hint.

    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, in a really busy environment, your No Lock query could say there's table corruption when there really isn't. I cannot remember the exact cause but it's to do with the nolock allowing another query to change the linked pages, etc and then when the nolock query comes across that bit it doesn't necessarily see the change straight away.

    Run your DB with nolock to see how it goes. If need be you should probably change the transaction isolation level rather than explicitly specifying locking hints.

  • If you are doing aggregation queries, where the consistency of the result is not critical, you should make NOLOCK your default choice. Also make NOLOCK your default choice when querying static data, as there is no need to get SQL to do more work then required. This often means that data warehouse (BI) queries are often a good candidate for NOLOCK.

    If you are doing queries against operational data (e.g. checking quantity in stock before promising delivery) then you need the protection of locking, otherwise you may make the wrong business decision. This means that OLTP type queries normally do not have NOLOCK or the application uses some form of optimistic update logic to handle the integrity issues. Even with OLTP applications, static data queries can use NOLOCK.

    Original author: https://github.com/SQL-FineBuild/Common/wiki/ 1-click install and best practice configuration of SQL Server 2019, 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005.

    When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist - Archbishop Hélder Câmara

  • Be aware that you can get duplicate data back in a query using NOLOCK.

    Itzik Ben Gan demoed this and you can find a code example here.

    http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/16/1345.aspx

    --Dave

  • You've got to watch those hints and use them sparingly, in SQL 2005 there are better alternatives than NOLOCK in T-SQL. Have you looked into snapshots?

    One gotcha to avoid, SQL 2008 and beyond "Specifying NOLOCK or READUNCOMMITTED in the FROM clause of an UPDATE or DELETE statement when applied to the target table of the statement." will no longer work.

    Joe

  • Does the WITH (NOLOCK) option work with sql server 2005 ?

    Or is it depricated ?

    I was facing some serious deadlock issues in a high transaction table, i was not bothered about inaccurate data, i added nolock to a select statement.

    Even after i added nolock directive to my select, this select statement becomes a deadlock victim at times.

    I do not understand why and how can this happen? ( a select with nolock is not locking anything at all, how does it participate in a dead lock situation ? )

    can someone throw some light on this please ?

    fyi. there could be other transactions that update the data that the select with nolock transaction is trying to select.

    Thanks in adv.

    appreciate your help.

  • Santa Ana (3/31/2009)


    Does the WITH (NOLOCK) option work with sql server 2005 ?

    Yes

    I do not understand why and how can this happen? ( a select with nolock is not locking anything at all, how does it participate in a dead lock situation ? )

    You're going to have to show us the code to be sure, but I have before seen deadlocks resulting from schema locks as a result if enabling and disabling triggers and even selects with a nolock have to respect a Sch-M lock.

    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 9 posts - 1 through 8 (of 8 total)

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