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
1
vote
1 answer

using 'with nolock' in transaction or when dropping table

I am trying clean a temporal table. the quickest way was to drop or truncate table then rollback and only include required rows. Now my issue is with the 'deadlock' of database. is there a way to use 'with nolock' for the database to not lock BEGIN…
Cheri Choc
  • 131
  • 2
  • 11
1
vote
3 answers

Using WITH(NOLOCK) to increase performance

I have seen developers using WITH(nolock) in the query, is there any disadvantage of it? Also, what is the default mode of execution of query? My database do not have any index. Is there any other way to increase database select statement…
Chris
  • 2,293
  • 11
  • 48
  • 86
1
vote
1 answer

TransactionScope in business layer for dirty async read

I'm developing a .NET Core web api service and have the following method in BL: public async Task GetParams(CreateRequest request) { var user = await _userRepository.GetUserByLogin(request.Login); var…
Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45
1
vote
1 answer

When modifying linq-to-sql commandtext, should the connection be closed?

I'm modifying the commandtext of linq-to-sql to force it to use nolock, like this... if (db.Connection.State == System.Data.ConnectionState.Closed) db.Connection.Open(); var cmd = db.GetCommand(db.Customers.Where(p => p.ID ==…
peterorum
  • 1,401
  • 2
  • 15
  • 21
1
vote
0 answers

Temp Variable Assignment WITH (NOLOCK)

Is there a speed benefit to using WITH (NOLOCK) in temporary variable assignment. Meaning: This DECLARE @oldestPerson INT = (SELECT TOP 1 Age FROM tbl_Persons) Verses DECLARE @oldestPerson INT = (SELECT TOP 1 Age FROM tbl_Persons WITH…
Joe McCarty
  • 145
  • 1
  • 2
  • 8
1
vote
0 answers

How to determine records being removed in another transaction in SQL Server?

I have a table in SQL Server having ID column, which is also set as unique clustered index. I need to determine which records among given IDs are locked as being removed in another transaction. If I use READPAST I get only those which are not…
Rauf
  • 312
  • 3
  • 16
1
vote
2 answers

How do you use NOLOCK with TADOQuery and TADOTable?

I have the SQL text "SELECT * FROM TABLE1 WITH (NOLOCK)". Two questions: How do I make my TADOQuery use the NOLOCK hint without having to include that in the SQL text? I have literally thousands of TADOQuery's with their SQL dynamically built, and…
C. Smith
  • 149
  • 5
1
vote
1 answer

Hibernate: force 'with (NOLOCK)' hint on all select queries, without changing isolation level

Quick background story: I work on a very old application that has recently been having issues with locks on the database. The app is written in Java and uses Hibernate. One of the issues we identified are transactions that are kept alive unnaturally…
1
vote
2 answers

TSQL NOLOCK VIEW and stored procedure

In our company we tend to use views and stored procedures. We've recently started to implement the NOLOCK statement to a lot of our views. I was wondering: if I am applying NOLOCK to a view, it "trickles down" to the stored procedure Say I have a…
Mike
  • 2,547
  • 1
  • 24
  • 36
1
vote
1 answer

How to apply lock on table level in SQL server

I am using SQL server 2008 R2 edition and want to apply lock on table level while selecting the data from the table. As applying NO_LOCK can led to DIRTY READ problem so want to apply NO_LOCK on the tables that contains only domain data not the…
Tarun Arora
  • 113
  • 2
  • 3
  • 9
1
vote
1 answer

How to test MySql READ UNCOMMITTED usage for nolock

I'm using the following query: SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ; SELECT * FROM TABLE_NAME ; SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ ; The table has lots of write operations and heavy read operations. To get…
Buntu Linux
  • 492
  • 9
  • 19
1
vote
0 answers

NOLOCK at query level

I've a query to run a SQL server table, in which, I am not concerned with the exact count. So, I want to specify NOLOCK for that query. But, query itself consists of multiple subqueries and multiple joins; so, it's quite dirty to put NOLOCK all…
1
vote
2 answers

Can I apply NOLOCK with a database-wide setting?

Is there a way to make WITH(NOLOCK) be applied on any SELECT statement run on a particular database?
BeemerGuy
  • 8,139
  • 2
  • 35
  • 46
1
vote
2 answers

SQL server with (NOLOCK) hint

Could someone explain if using with (NOLOCK) in SQL queries actually cause them to run faster? And if so, why?
slayernoah
  • 4,382
  • 11
  • 42
  • 73
1
vote
0 answers

SQL Server requests shared lock on a page in a statement with nolock hint

Subj. Look at the deadlock graph and the statement below. Why does it happen? declare @SomeValue bigint = 12345 select from [HealthEvent].[StateMachineInstanceEventTypeQueue] nolock where SomeField = @SomeValue order by id…