I know with(nolock)
and (nolock)
are the same or almost the same. REF:with(nolock) or (nolock) - Is there a difference?
but how about nolock
? You can use either of them in select
and the only notable difference I can see is when using alias that you can write:
select * from table1 as mytable with(nolock)
or
select * from table1 as mytable (nolock)
but you can't write:
select * from table1 as mytable nolock
PS: I'm not discussing nolock
is good or bad here :)
The WITH (NOLOCK) table hint is used to override the default transaction isolation level of the table or the tables within the view in a specific query, by allowing the user to retrieve the data without being affected by the locks, on the requested data, due to another process that is changing it.
What does the SQL Server NOLOCK hint do? The NOLOCK hint allows SQL to read data from tables by ignoring any locks and therefore not get blocked by other processes. This can improve query performance by removing the blocks, but introduces the possibility of dirty reads.
The only difference between the two is that the READ UNCOMMITTED isolation level determines the locking mechanism for the entire connection and the NOLOCK table hint determines the locking mechanism for the table that you give the hint to.
Understanding NOLOCK Hint The benefits of querying data using the NOLOCK table hint is that it requires less memory and prevents deadlocks from occurring with any other queries that may be reading similar data.
The difference is that you should be using the syntax WITH (NOLOCK)
(or WITH (<any table hint>)
). Why?
Without WITH
is deprecated. From Table Hints on MSDN:
Omitting the WITH keyword is a deprecated feature: This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
from table1 nolock
does not apply a hint at all - that's an alias. For example:
SELECT nolock.name FROM sys.objects nolock ORDER BY nolock.name;
Notice that I can use nolock
as an alias. No hint is applied here.
from table1 as mytable nolock
is invalid syntax in modern versions of SQL Server.
Msg 1018, Level 15, State 1, Line 12
Incorrect syntax near 'nolock'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.
You should also consider using the session-level hint, SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
, because then you don't have the syntax issue above, and you also don't have 15 WITH (NOLOCK)
s littering your query. These make it harder to replace with a different isolation level later (like RCSI, which is far more practical than READ UNCOMMITTED
IMHO), whereas the single batch-level statement is a very easy one-liner to replace.
Also, and this is for other readers more so than for the OP, please be absolutely sure you understand the risks of using NOLOCK
, which include getting corrupted data in a single row that never existed:
The table hint part of the select query has the following syntax:
WITH ( <table_hint> [ [, ]...n ] )
http://technet.microsoft.com/en-us/library/ms187373.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With