What is (are) difference between NOLOCK and UNCOMMITTED

SqlSql ServerSql Server-2012LockingIsolation Level

Sql Problem Overview


I use SQL Server 2012.

I write two queries but what is a different between NOLOCK and UnCommitted ?

SELECT lastname, firstname
FROM HR.Employees with (READUNCOMMITTED)

SELECT lastname, firstname 
FROM HR.Employees with (NoLock)

Sql Solutions


Solution 1 - Sql

NOLOCK : Is equivalent to READ UNCOMMITTED (source : MSDN)

NOLOCK or READ UNCOMMITTED 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

READ UNCOMMITTED and NOLOCK hints apply only to data locks. All queries, including those with READ UNCOMMITTED 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

Solution 2 - Sql

No difference in terms of their functions, like other have mentioned.

The single difference is that you can apply WITH(NOLOCK) selectively, on some tables but not others. READ UNCOMMITTED applies NOLOCK to all tables in a session.

If you do this:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT *
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.ID = T2.id

It is functionally equivalent to:

SELECT *
FROM Table1 T1 WITH(NOLOCK)
INNER JOIN Table2 T2 WITH(NOLOCK) ON T1.ID = T2.ID

But you can also apply WITH(NOLOCK) selectively:

SELECT *
FROM Table1 T1 WITH(TABLOCK)
INNER JOIN Table2 WITH(NOLOCK) ON T1.ID = T2.ID

Solution 3 - Sql

Under the hood they are the performing the same action.

The READ UNCOMMITTED isolation level is the least restrictive isolation level within SQL Server, which is also what makes it popular for developers when looking to reduce blocking.

The NOLOCK table hint behind the scenes performs the exact same action as running under the read-uncommitted isolation level.

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.

Solution 4 - Sql

There is no difference at the statement level.

You can set READUNCOMMITED at the session level and here you have to write SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

Solution 5 - Sql

For NOLOCK , we need to put this hint on table level, so it is require to put for every tables level which are used in update transaction. So it is very lengthy and time consuming to put it everywhere tables refers in query. For READ UNCOMMITTED, We do not need to put it every tables level, just put at session level or query level and can be written at top of the query or stored procedure. Let us look on small demo to elaborate it. First checking here database default isolation level

CREATE TABLE SAMPLETABLE
(
Col1 INT ,
Col2 VARCHAR(100)
)

INSERT INTO SAMPLETABLE(Col1,Col2)
SELECT 1,'Col1'
Union all
SELECT 2,'Col1'

BEGIN TRANSACTION 

Update SAMPLETABLE Set Col2 = 'Value changed' Where col1 =1

Select * from SAMPLETABLE with (nolock)



SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

Select * from SAMPLETABLE 

Output is 1, Col1 for both query

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArdalan ShahgholiView Question on Stackoverflow
Solution 1 - SqlPரதீப்View Answer on Stackoverflow
Solution 2 - SqlCode DifferentView Answer on Stackoverflow
Solution 3 - SqlPrabuView Answer on Stackoverflow
Solution 4 - SqlCPMunichView Answer on Stackoverflow
Solution 5 - SqlM PrabhuView Answer on Stackoverflow