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
3
votes
1 answer

SQL Server NOLOCK with JOIN, Bulk load

Following is the scenario I have: I have a stored procedure that returns data by joining 4 tables. Twice in the middle of the day there is a bulk upload to one of the above 4 tables. The load continues for 10-15 minutes. I do not want the UI that…
Viking22
  • 545
  • 1
  • 7
  • 19
2
votes
2 answers

High Traffic SQL Table intermittent Null Output Parameter

I'm getting an intermittent null value for an output parameter for a stored procedure I have. I'm wondering if it has to do with the NOLOCK inside the stored procedure. It works most of the time, it's intermittently failing. Especially under high…
Robert
  • 568
  • 1
  • 6
  • 21
2
votes
1 answer

SQL Server: VIEW with NO LOCK but called without NO LOCK

I am on Microsoft SQL Server 2016 and I have the following (simplified) query that references a view: SELECT Column1 FROM dbo.myView The view looks as follows: CREATE VIEW dbo.myView AS SELECT Column1 FROM dbo.SomeOtherTable WITH (NOLOCK) My…
beta
  • 5,324
  • 15
  • 57
  • 99
2
votes
1 answer

In LINQ-To-SQL, should I use NOLOCK to improve performance?

Our DBA came to us with information that our LINQ queries are creating many thousands of locks on the database. A developer on our team dug up this Hanselman post as a possible solution to our…
John
  • 3,332
  • 5
  • 33
  • 55
2
votes
0 answers

JPA sql server 'nolock' without transaction

I searched for ways to query with nolock from JPA, but I get errors saying I'm not in a transaction, which is true. Is it possible to turn off locking without a transaction?
2
votes
3 answers

Do I get any performance gain by using WITH (NOLOCK) on SQL Server database where READ_COMMITTED_SNAPSHOT is switched on?

I have a database on Microsoft SQL Server 2014 where READ_COMMITTED_SNAPSHOT property is switched on. I understand that this means that reads are not blocked by writes because reads do not issue shared locks and that this is clean reading. My…
Jiri Necas
  • 33
  • 6
2
votes
3 answers

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,…
JohnG
  • 272
  • 2
  • 14
2
votes
0 answers

When does the value for a rowversion column get updated?

If I have a table with a rowversion column and it gets updated as follows: Name VersionStamp Before update: Bob ABCD After update: Dave EFGH What happens if someone queries the table…
2
votes
1 answer

In my circumstance NOLOCK, Snapshot or something else?

I have a SQL Server 2012 table that will contain 2.5 million rows at any one time. Items are always being written into the table, but the oldest rows in the table get truncated at the end of each day during a maintenance window. I have .NET-based…
tommed
  • 1,521
  • 2
  • 19
  • 33
2
votes
3 answers

Understanding the NOLOCK hint

Let's say I have a table with 1,000,000 rows and running a SELECT * FROM TableName on this table takes around 10 seconds to return the data. Without a NOLOCK statement (putting to one side issues around dirty reads) would this query lock the table…
Remotec
  • 10,304
  • 25
  • 105
  • 147
2
votes
4 answers

What is purpose of using NOLOCK on a SELECT statement?

Can anyone suggest a good reason to issue a NOLOCK on a SELECT statement? I am re-factoring some stored procedures that are littered with them and it is my understanding that a NOLOCK on a SELECT statement is all but useless.
rstuppi
  • 361
  • 1
  • 3
  • 11
1
vote
1 answer

with nolock and exclusive locks

So, with nolock will not take any locks. Will it still honor outstanding locks? For example, if I attempt to select a row with nolock, which another process has put an exclusive lock on, will my select with nolock be blocked, or will it select the…
aepheus
  • 7,827
  • 7
  • 36
  • 51
1
vote
1 answer

How can I read dirty values in SQL UPDATE statement WHERE clause

Let's assume I have the following query in two separate SSMS query windows: SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED BEGIN TRANSACTION UPDATE dbo.Jobs SET [status] = 'Running' OUTPUT Inserted.* WHERE [status] = 'Waiting' --I'm NOT committing…
Joseph Goldberger
  • 313
  • 1
  • 3
  • 10
1
vote
1 answer

support for nolock in SQL Azure

Does anyone have an idea as to why SQL Azure does not support with nolock SQL statements. Does Microsoft has any plans of supporting this in the future?
coderman
  • 1,485
  • 1
  • 16
  • 15
1
vote
1 answer

Nolock as default for Create One ObjectContext per Request for EF4

Is there any side effect for this code: ///This code runs per request public static MyObjectContext CreateEntity() { MyObjectContext db=new MyObjectContext(); db.Connection.Open(); var con =…