Questions tagged [nolock]

A SQL Server table hint that can be used to prevent queries from issuing shared read locks. While often used to prevent a query from blocking other queries it also makes it susceptible to dirty reads and other potential data issues.

From the SQL Server Books Online topic on Table Hints:

Is equivalent to READUNCOMMITTED.


Specifies that dirty reads are allowed. No shared locks are issued to prevent other transactions from modifying data read by the current transaction, and exclusive locks set by other transactions do not block the current transaction from reading the locked data. Allowing dirty reads can cause higher concurrency, but at the cost of reading data modifications that then are rolled back by other transactions. This may generate errors for your transaction, present users with data that was never committed, or cause users to see records twice (or not at all).

READUNCOMMITTED and NOLOCK hints apply only to data locks. All queries, including those with READUNCOMMITTED and NOLOCK hints, acquire Sch-S (schema stability) locks during compilation and execution. Because of this, queries are blocked when a concurrent transaction holds a Sch-M (schema modification) lock on the table. For example, a data definition language (DDL) operation acquires a Sch-M lock before it modifies the schema information of the table. Any concurrent queries, including those running with READUNCOMMITTED or NOLOCK hints, are blocked when attempting to acquire a Sch-S lock. Conversely, a query holding a Sch-S lock blocks a concurrent transaction that attempts to acquire a Sch-M lock.

READUNCOMMITTED and NOLOCK cannot be specified for tables modified by insert, update, or delete operations. The SQL Server query optimizer ignores the READUNCOMMITTED and NOLOCK hints in the FROM clause that apply to the target table of an UPDATE or DELETE statement.


You can minimize locking contention while protecting transactions from dirty reads of uncommitted data modifications by using either of the following:

  • The READ COMMITTED isolation level with the READ_COMMITTED_SNAPSHOT database option set ON.
  • The SNAPSHOT isolation level.
89 questions
680
votes
16 answers

What is "with (nolock)" in SQL Server?

Can someone explain the implications of using with (nolock) on queries, when you should/shouldn't use it? For example, if you have a banking application with high transaction rates and a lot of data in certain tables, in what types of queries would…
Andy White
  • 86,444
  • 48
  • 176
  • 211
183
votes
3 answers

SQL Server NOLOCK and joins

Background: I have a performance-critical query I'd like to run and I don't care about dirty reads. My question is; If I'm using joins, do I have to specify the NOLOCK hint on those as well? For instance; is: SELECT * FROM table1 a WITH…
DanP
  • 6,310
  • 4
  • 40
  • 68
132
votes
12 answers

Is the NOLOCK (Sql Server hint) bad practice?

I'm in the business of making website and applications that are not mission critical -> eg. banking software, space flight, intensive care monitoring application, etc. You get the idea. So, with that massive disclaimer, is it bad using the NOLOCK…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
73
votes
5 answers

PostgreSQL Equivalent of SQLServer's NoLock Hint

In SQLServer, you can use syntax "(nolock)" to ensure the query doesn't lock the table or isn't blocked by other queries locking the same table. e.g. SELECT * FROM mytable (nolock) WHERE id = blah What's the equivalent syntax in Postgres? I found…
Cerin
  • 60,957
  • 96
  • 316
  • 522
52
votes
3 answers

with(nolock) or (nolock) - Is there a difference?

Everything is based on the assumption that with(nolock) is entirely appropriate for the situtation. There are already plenty of questions out there debating whether or not to use with(nolock). I've looked around and haven't been able to find if…
Rob
  • 834
  • 1
  • 10
  • 15
38
votes
6 answers

Remote table-Valued Function Calls are not allowed

How can I make this work?Im running a table valued function from a remote linked server. i tried adding no lock to this 4 part naming but still i get the same error. Im using mssql-2008 select * from…
anonymous1110
  • 885
  • 4
  • 14
  • 28
22
votes
5 answers

syntax for nolock in sql

I have seen sql statements using nolock and with(nolock) e.g - select * from table1 nolock where column1 > 10 AND select * from table1 with(nolock) where column1 > 10 Which of the above statements is correct and why?
seenasan
  • 221
  • 1
  • 2
  • 3
20
votes
5 answers

SQL Server - how to set (nolock) hint as a default?

is there some way to tell sql server to use the (nolock) hint or every select in a stored procedure? is pretty tiresome to add it to each an every select....
opensas
  • 60,462
  • 79
  • 252
  • 386
11
votes
3 answers

Java Hibernate HQL queries with nolock

Is there a way to run these queries as if I added a (NOLOCK) hint to them?
Sarit
  • 897
  • 3
  • 9
  • 18
10
votes
6 answers

When is it appropriate to use NOLOCK?

I am having timeout issues and deadlocks from time to time with some long running queries. I'm wondering when is it most appropriate to use NOLOCK and where? Do I use it on the updates & inserts? or reads?
Brian Hedler
  • 559
  • 6
  • 9
9
votes
1 answer

MS SQL Server 2008 "with (nolock)" equivalent for IBM DB2 9.7

In MS SQL Server 2008 you can write like this: FROM EMPLOYEE as A with (nolock) Is there an equivalent syntax for DB2 9.7? Thanks
user1340582
  • 19,151
  • 35
  • 115
  • 171
8
votes
3 answers

Update Query with Nolock hint

I am trying to put an with(NOLOCK) on an update query: UPDATE pth_patchLookup with(nolock) SET ScanDateTime = Getdate() WHERE RegID = 312 but I get the following message : NoLock hint is supported only with Select statement and not with update,…
Ram Mehta
  • 449
  • 1
  • 6
  • 20
8
votes
6 answers

Using NOLOCK Hint in EF4?

We're evaluating EF4 and my DBA says we must use the NOLOCK hint in all our SELECT statements. So I'm looking into how to make this happen when using EF4. I've read the different ideas on how to make this happen in EF4, but all seem like a work…
John
  • 121
  • 1
  • 2
  • 4
8
votes
7 answers

In a Data Warehouse scenario is there any disadvantage to using WITH(NOLOCK)

I have a Kimball-style DW (facts and dimensions in star models - no late-arriving facts rows or columns, no columns changing in dimensions except expiry as part of Type 2 slowly changing dimensions) with heavy daily processing to insert and update…
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
6
votes
1 answer

Does NOLOCK hint slow down operation?

I have a question about the use of NOLOCK. I understand that NOLOCK hint is not always the best approach, but in some case it is very helpful. I am not trying to build a bad habit of using it all the time I just want to understand the exact…
Junior
  • 11,602
  • 27
  • 106
  • 212
1
2 3 4 5 6