Search code examples
sql-serverdeadlocknolock

Is the With(NoLock) hint dangerous in my case?


I've read and used with(nolock) hints many times before but I have a question for a specific case.

In my case I have one set of code that refers and updates one database.
This code was meant to run in a single threaded fashion. A few months ago, they decided to make it multi-threaded without changing the code. The way they went about it, is to make each different "code processor" manage a different group of stores.

For example, processor1 takes care of stores 1 to 20, processor2 takes care of stores 21 to 40 and so on.

Everything seemed fine until we started getting deadlocks. The deadlocks are always on pages... if the locks were only on rows we would never get deadlocks since the data from one processor never conflicts with the data from another processor. Based on that theory, I decided to put With(Nolock) hints on every select that doesn't need to lock (hasn't been put in prod yet)... which made my deadlocks disappear in my Lab.

Everything seemed fine and dandy until my colleague came up to me with THIS article which scared the crap out of me... then I read THAT...

Is there a danger to use the With(Nolock) hint in my case... where data will never conflict between processors?


Solution

  • Yes, there could well be issues if you are collecting data that has to be accurate - you may grab "dirty" / incorrect data and then persist it. Have you looked at whether indexing can correct the deadlock problem instead? Often a deadlock scenario can be solved by controlling the access pattern to the data pages, which is controlled by the available indexes.

    What I like to do is look at the deadlock graph to see where the conflict is, then look at the order of operations in the code, and which indexes are being used to access/modify the data, to see if I can adjust to remove the deadlock risk.