Questions tagged [nolock]

NOLOCK is a SQL Server table hint, equivalent to READUNCOMMITTED. It applies READ UNCOMMITTED isolation level semantics to the hinted table.

NOLOCK and READUNCOMMITTED are equivalent SQL Server table hints, used to apply READ UNCOMMITTED transaction isolation semantics to the hinted table. Other object references in the query are generally unaffected, retaining the isolation level in effect for the statement as a whole.

Under the READ UNCOMMITTED isolation level, transactions can see uncommitted changes made by other concurrent transactions, even if those changes are eventually aborted (rolled back). SQL Server's implementation of READ UNCOMMITTED also allows duplicate reads, partial reads of LOB data types, skipped rows, and statements may also fail completely with error 601:

"Could not continue scan with NOLOCK due to data movement."

Reads performed until the READ UNCOMMITTED isolation level do not generally acquire shared (S) locks (except on the HoBt when reading a heap) and do not respect update (U) or exclusive (X) locks set by other transactions changing data.

Schema stability (Sch-S) locks are still taken during query compilation and execution. A concurrent transaction that takes a schema modification lock (Sch-M), for example by issuing a TRUNCATE TABLE or DDL statement will still block a read with NOLOCK. Similarly, a transaction reading a structure at the READ UNCOMMITTED isolation level will block other processes needing to acquire a Sch-M lock (see the lock compatibility matrix in Books Online).

The READUNCOMMITTED and NOLOCK table hints cannot be applied directly to the target table in INSERT, UPDATE, DELETE, or MERGE operations. The SQL Server optimizer ignores READUNCOMMITTED and NOLOCK hints on any aliases of the target table in the FROM clause of a INSERT, UPDATE, DELETE, or MERGE statement. As well as being ineffective, the ability to specify this hint against an alias of an updated table is deprecated and will be removed from a future version of SQL Server.

Many applications that currently rely on NOLOCK hints to achieve higher concurrency (most commonly to avoid readers blocking writers) can achieve the same effects by switching to the READ COMMITTED isolation level with the READ_COMMITTED_SNAPSHOT database option set ON, or by changing to the SNAPSHOT isolation level.

For more information on isolation levels, see SET TRANSACTION ISOLATION LEVEL in Books Online. More information about row-versioning isolation levels can be found in the topic Locking and Row Versioning and subsections.

30 questions
35
votes
4 answers

Is NOLOCK always bad?

I am a Report Developer who wants to make my queries as efficient as possible. I used to work with a DBA who told me - I believe because I was always dealing with reports on a Production Server - to use NOLOCK in every single query. Now, I work…
DataGirl
  • 475
  • 4
  • 6
24
votes
1 answer

(NOLOCK) vs NOLOCK

I was investigating some blocking when I saw a query that looked something like this: SELECT SomeField FROM SomeTable NOLOCK I saw the NOLOCK and was curious how it could be blocking other queries, in this case DELETE statements. I took a quick…
Brian
  • 353
  • 2
  • 5
23
votes
3 answers

Justify NOT using (nolock) hint in every query

Have you ever had to justify NOT using a query hint? I am seeing WITH (NOLOCK) in every single query that hits a very busy server. It is to the point that the developers think it should just be on by default because they hate seeing it in their…
datagod
  • 7,141
  • 4
  • 38
  • 58
11
votes
1 answer

Use NOLOCK hint when calling table valued function

Suppose the following function: CREATE FUNCTION [dbo].[ufnTest]() RETURNS TABLE AS RETURN SELECT 1 AS Nr My actual function will select actual data from many different tables. I (mostly) understand the risks of using nolock hints, and decided that…
Jeroen
  • 909
  • 2
  • 14
  • 34
11
votes
1 answer

How to reproduce "Could not continue scan with NOLOCK due to data movement"

I occasionally get "Could not continue scan with NOLOCK due to data movement" with some large jobs, which do have WITH (NOLOCK) on the select queries. I understand this has something to do with attempting to select data when there has been a page…
wookie23
  • 111
  • 1
  • 4
10
votes
2 answers

Could not continue scan with NOLOCK due to data movement

We run SQL Server 2000 and we get a few of these errors every night. Could not continue scan with NOLOCK due to data movement The query that throws this error is a large complex query that joins over a dozen tables. Our underlying data can be…
Ciaran Archer
  • 395
  • 2
  • 3
  • 10
9
votes
2 answers

Safe to use WITH (NOLOCK) in this case?

Scenario I have a table that deals with concurrent SELECT's and DELETE's. I'm getting a few deadlocks on my SELECT statements. I assume that the DELETE from the other transaction is getting an exclusive lock and conflicting with the shared lock of…
trademark
  • 213
  • 3
  • 8
8
votes
1 answer

How can I prove NOLOCK is the source of deadlocking problems?

I'm not trying to start a windows/mac type discussion. Personally, I don't need any convincing that NOLOCK is not a good idea as a reflexive practice. It seems when you're developing everything should be purposeful not reactionary (/amen) So...…
user238855
  • 363
  • 1
  • 2
  • 9
7
votes
1 answer

Does With(NoLock) help with query performance?

Most of the select statements in our stored procedures use WITH(NOLOCK) and I was told by my co-workers that they use it to improve performance. Now, I know that this hint helps with locking but does it actually improve performance? I've read…
RobMartin
  • 159
  • 1
  • 7
7
votes
1 answer

What are the problems with using NOLOCK given an approach to handle the short comings?

I am researching the harm of using NOLOCK SQL to load data from one database that is actively used into a reporting database. I understand that there are problems with using NOLOCK but I am thinking of ways to counter them using the strategy…
variable
  • 3,590
  • 4
  • 37
  • 100
5
votes
1 answer

repeatable read and nolock

Does anyone know what the net effect of having a block of t-sql with a transaction isolation level of repeatble read and then inside having a select with nolock. Does the nolock become invalid because the isolation level has already been set?
blakmk
  • 51
  • 1
  • 2
5
votes
3 answers

How to avoid a select query for holding a Sch-S lock

I am looking for an option to avoid Sch-S locks when running a Select query. I have a database which is controlled by an application written by others as well as my own application. One of the tables has millions of rows. I don't have any issue with…
AKS
  • 151
  • 1
  • 1
  • 4
5
votes
3 answers

Why is NOLOCK implemented like that?

Recently I have searched for NOLOCK option in SQL Server. What I've discovered is that when a transaction is active on a table, SQL Server does not allow even reading from a specific table until the transaction is either committed or rolled back (as…
Dimitrios Desyllas
  • 873
  • 2
  • 14
  • 30
4
votes
1 answer

Skipped rows or rows read multiple times with NOLOCK

Is it possible to stimulate a situation where rows either get skipped or get read multiple times in a select statement? E.g. the way we can cause a blocking or a deadlock...
xhr489
  • 827
  • 11
  • 30
4
votes
2 answers

Can someone explain why select with nolock will query a potion of updated data?

I was reading the answer from here (from stackoverflow, I think should ask in here) NOLOCK means placing no locks at all. Your query may returns portions of data as of before UPDATE and portions as of after UPDATE in a single query. I get that…
King Chan
  • 399
  • 2
  • 5
  • 11
1
2