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
0 answers

Deadlock between select WITH (NOLOCK) and replication agent

I experienced a deadlock between the database replication agent and a select statement that uses the nolock hint. The select statement was chosen as the victim. Here is the select statement, and as you can see it uses the nolock hint on both objects…
supergrady
  • 1,322
  • 10
  • 13
0
votes
3 answers

C# T-SQL statement includes "with(nolock)" error

Short: SQL statement in my C# code is not working. with(nolock) is breaking the code. Detailed: Below are my errors and the code where I am getting the error. The code is supposed to connect to my SQL Server database (connection code works fine)…
toosweetnitemare
  • 2,226
  • 8
  • 33
  • 44
0
votes
0 answers

SQL Timed out and Long running session

We are on SQL Server 2017 Application was set to run PROC up to 5min. So if it exceed 5mins it is going to timed out. The PROC got timedout, when I see the history of that query in sp_whoisactive which we store history in our log table): the reads,…
0
votes
0 answers

Does Prisma support select with "with (nolock)"?

How to use "with (nolock)", usually used in select in SQL Server in Prisma?
0
votes
2 answers

NOLOCK in Hive and Impala

I want to understand why we don't use the NOLOCK keyword at the end SQL queries that run in Impala, Hive or MySQL. For example, I use both Hive and Impala through the Cloudera workbench at work. Cloudera workbench does not recognise the NOLOCK…
Excel-lit
  • 71
  • 9
0
votes
0 answers

Predicate CONTAINS doesn't return records with NOLOCK

I am writing integration tests and I use TransactionScope(TransactionScopeAsyncFlowOption.Enabled) for this purpose. I add some data to DB (MS SQL Server) and then check the result, compare it etc. When the test is passed, new data will not be saved…
0
votes
0 answers

Best approach to Read bulk records and update individual records without locking table

Here is my code and I am looking for best approach without locking records while updating. Declare @maxRowId int Set @maxRowId=0 Declare @rowIndex int Set @rowIndex=1 Declare @recordId bigint Set @recordId=0 Begin Try Declare…
techSC
  • 1
0
votes
1 answer

How to combine two different SQL queries in one SQL statement?

I have two tables which are: Members Member_id | Member_user_name | Member_account_id and Members_transaction Member_id (sender) | Member_transaction_id | Member_account_id (recipient) | Amount | from_to All columns have data but from_to is a…
Mouris
  • 67
  • 1
  • 8
0
votes
2 answers

Alternative for uncommitted reads in oracle database

I had a problem with a query in oracle 10g database when two users launch a process at the same time. This query is inserting volume data from another table. I tried to simplify the example: Tables structure: Table 1 VAL1 | VAL2 Table 2 ID | VAL1 |…
0
votes
1 answer

The use WITH(NOLOCK) in SQL Server

I have a question about the use of WITH (NOLOCK / READPAST) in SQL Server. When I used the WITH (NOLOCK / READPAST) hint on one or two tables, and I have other tables in JOINs, should I use that hint for all tables, or just the tables that I know…
user8223022
  • 195
  • 2
  • 15
0
votes
0 answers

modifying existing code to adapt nolock in jpa

What I want to achieve is, I would like to do a no lock query execution in select statement. But according to this answer it is impossible to achieve this with direct JPA implementation. I also understood from searches that nolock and…
arjuncc
  • 3,227
  • 5
  • 42
  • 77
0
votes
1 answer

Using WITH (NOLOCK) on some tables in a JOIN query

I've read about this and transaction isolation level and was just wondering..... I have some queries that query against products, categories, locations & stock. Now the products, categories and locations data rarely changes, but as you can imagine,…
AntDC
  • 1,807
  • 14
  • 23
0
votes
1 answer

Bad SQL grammar with statements using NOLOCK using HSQLDB

I am using HSQLDB and am trying to run a select statement which contains NOLOCK in the query. When the statements are exeuted, a BadSqlGrammarException is thrown. Is there a way to get around this issue as the command runs perfectly well with SQL…
Biscuit128
  • 5,218
  • 22
  • 89
  • 149
0
votes
2 answers

SQL SERVER, will select query with (TABLOCKX) faster than with(NOLOCK) or vice versa?

I had question above question. We are using with nolock through out the application. In some cases I need to work select faster, what ever the effect. So select with(TABLOCKX) will be faster or with(nolock)?
Amol Patil
  • 985
  • 2
  • 11
  • 43
0
votes
1 answer

Can an Insert/update followed by a select (nolock) yield unexpected results with same data?

In a stored procedure, can an insert/update followed by a select (nolock) on the same record yield unexpected results? Do I always get the (complete) updated/inserted record? Thanks in advance for your help!
Gerard
  • 2,649
  • 1
  • 28
  • 46