Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT Statement - NOLOCK with SET TRANSACTION ISOLATION LEVEL READ COMMITTED

My understanding is that when NOLOCK is used in SELECT statement, it could read uncommitted / dirty rows as well. But I wanted to take advantage of NOLOCK hint on table so that my SELECT query will run fast.

Now, does NOLOCK on table but along with "SET TRANSACTION ISOLATION LEVEL READ COMMITTED" give me NOLOCK advantage and faster SELECT query (because of NOLOCK) with only committed rows (because of SET)?

like image 354
Dave Avatar asked Oct 04 '10 16:10

Dave


1 Answers

yes a table hint overrides the isolation level setting, so you will still get dirty reads

easy to test

first run this

create table bla(id int)
insert bla values(1)



begin tran
select * from bla with (updlock, holdlock)

make sure not commit the tran!! open another window and run this

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

select * from bla -- with (nolock)

you don't get anything back.

open another window and run this now

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

select * from bla with (nolock)

as you can see you will get back the row

BTW, READ COMMITTED is the default isolation level, no need to set it

Take a look at Snapshot Isolation which won't give you back dirty data but still won't lock

like image 88
SQLMenace Avatar answered Oct 13 '22 07:10

SQLMenace