Change mssql isolation level to read uncommitted data

Published 29 September 6 2:43 PM | Ramon Smits

Sometimes you are debugging an application and want to query the database while the database connection still has not committed (or rollbacked) the transaction. So you fire up the query analyzer just to find out that you cannot see the modifications yet (ofcourse). Today a collegue of mine (Frank Bakker) found out that it is possible to read uncommitted data by altering the isolation level on a connection.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

When you have executed this statement you can now see all modifications that are still in a transaction. Isnt that neat or what?

The thing is that I used this some years ago in some functionality that generates reports based on website visits. The query did a select statement and it wouldn't be bothered if some additional records would have beed added to the table. You can get this behaviour by adding the keyword NOLOCK like the following example.

SELECT count(*)
FROM MyTable NOLOCK
GROUP BY MyCoolColumn

So NOLOCK and SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED are the same but the first (NOLOCK) is usualy best option as it is directly visible that the query needs a different isolation level. Setting the isolation level seperately can ofcourse increase readability is your query does a lot of joins though.

You can also do this in MySql in almost the same syntax as shown below.

SELECT count(*)
FROM MyTable(nolock)
GROUP BY MyCoolColumn

Happy 'reading'...

Comments

# d said on November 1, 2006 10:32 AM:

Ramon, Have you tested your work/? It appears to apply ONLY to mySQL Server??? I can't get NO LOCK to be recognized by the mySQL parser. If you know how, please email me at dgaedcke [at] gmail.com

# Ramon Smits said on December 19, 2006 1:26 AM:

I just read Dennis post about his adventures in isolation level land . He says he does not know a good