NOLOCK hits Mythbusters!

Exactly one month to this very day I wrote the article “When should you use NOLOCK?” which explained exactly when the use of the READUNCOMMITTED isolation level was acceptable and when it wasn’t. I also revealed that it not only takes out a Schema Stability (Sch-S) lock on the object (which is documented in Books Online for the hint) but also took out a special Shared (S) Bulk Operation lock when used on heaps. This latter behaviour is not even mentioned in Books Online for the hint and as far as I know, has never been documented or mentioned anywhere else before.

I therefore asked Paul Randal to check out my aforementioned blog post to confirm that I wasn’t going mad, and thankfully he confirmed that I wasn’t and that it was in fact expected behaviour. He also thanked me for giving him the idea for his final myth for his “More DBA Mythbusters(DBA-316-S)” session which he performed yesterday in which he gave and explanation of these behaviours. So I not only encourage you to check out my post, but to get hold of the PASS Summit 2011 DVD set and have a watch!

My idea hits Mythbusters!

Posted in Concurrency, sql, SQLServerPedia Syndication | 1 Comment

Community is driving SQL to success

Well the keynote speech has begun, Ted Kummert is on stage and the focus at the beginning has been very much on the Community and SQL Azure. It is clear to me that Microsoft recognize the value of our amazing community -and they should because together we are helping to shape that future of this product into something that is becoming truly amazing. There was a time when Oracle DBA’s used to look down on SQL Server, but now they are worried and are desperately trying to get up to speed.

Ted Kummert on Stage

All the arguments that used to be given with respect to the supposed “failings” of our database platform have been slowly eroded away -a good example of this is our concurrency model. Microsoft have not only addressed criticisms and failings of our pessimistic concurrency model by constantly tweaking and providing various concurrency mechanisms such as database snapshots and scale-out strategies but have now provided optimistic concurrency by providing the new ISOLATION LEVEL as of SQL 2005 (Snapshot Isolation) and even gave us an optimistic implementation of READ COMMITTED isolation.

Whilst I am still undecided about the short term success of SQL Azure, one thing I know for sure is that through all of US this product is only going to become an even more amazing product. Join us and together we can make something special!

Posted in sql, summit_2011 | 1 Comment

My Alarm clock worked…

So the time is around 7:30 am and I managed to get out of bed for my second official appointment of the PASS Summit 2011 and things are getting warmed up. I’m sat here on the front row of the Blogger table whilst to my left I have Brent Ozar, Aaron Bertrand and to my right Wes Brown and the Midnight DBA’s. If that wasn’t enough in front of me I have Stacia Misner and behind me I have Wendy Dance and Glen Berry! I don’t know how I managed this gig but it feels really good and it is an absolute pleasure to be part of it all.

Room is starting to fill...

Well that’s my first post for the day, onto writing the next one!

Posted in sql, sqlpass, summit_2011 | 1 Comment

I’m gonna be on the Blogger’s Table!

So I’m currently sat here in the lounge of the Seattle Sheraton and finally have time to reflect upon my week ahead here at this years SQLPASS Summit. At this point in time it is very hard to say exactly how busy I am going to be this week but I suspect that my time here will be slightly more focused than last time since I have a few more responsibilities than before.

So my first official appointment will happen on Tuesday evening when I hope to meet and great my SQLPASS first timers having signed myself up to be a “Big Brother” to all those people that have never visited the Summit before. I really expect to learn a lot myself from this experience and if nothing else hope to learn some insights into becoming a better Big Brother for future conferences. I already have some ideas in this area, so for now I shall try and absorb what I can with this in mind.

Following this meeting (and the Welcome reception) I will be joining the PASS Speaker and Volunteer party and should be really fun to meet the people that are helping to make this conference actually happen. Some of which I know and many I don’t, so I am really looking forward to this event. It is just a shame that it has been arranged for the same night of my first timer reception and I have raised this point with the PASS organizers to try and avoid this same thing happening again.

Then on the Wednesday morning I am truly excited about being on the SQLPASS bloggers table. At this stage I am not entirely sure what this will entail and what exactly I will be doing there, but I presume that at the very least I shall be live blogging and tweeting about the SQLPASS Summit and the goings on around me. Should be good!

Finally on the Friday I will be giving my Lightning talk “SQL Server Clustering for Clustering for Dummies” which should be a really fun thing to do. I really need to figure out what I am going to say and it is really going to be a challenge to cram one hours worth of material into 5 minutes but I am up for this challenge!

That’s me for now, and all that there is left to say is that if you know me, know of me or want to know me (more fool you) then I really hope you come up and say hello and perhaps join me for a beer or two one evening!

Posted in personaldevelopment, publicspeaking, sql, sqlpass, SQLServerPedia Syndication | 9 Comments

SQLBits 9 Slidedeck for READPAST & Furious

I am very pleased to publish the slide-deck for my Friday presentation “READPAST & Furious: Transactions, Locking and Isolation“. Just click here to obtain the slide-deck in PDF format.

I had an absolutely fantastic time giving the presentation and really enjoyed the audience participation (though it took some of you guys a bit of time to warm up :) ). No seriously, the thing that makes all those weeks/ months of preparation and collation of material worthwhile is the possibility that I have managed to communicate something of value to you all -and hopefully I managed to do that. I learnt a massive amount of stuff myself in the process of preparing for this session and I cannot wait for the next time (whenever this may be).

Thank you very much to all those people who attended and especially those who have been to my presentations before. I can’t tell you how much I appreciate you being there!

Regards,

retracement.

Posted in Concurrency, performance, publicspeaking, sqlbits, SQLServerPedia Syndication, tuning | Leave a comment

Fancy a decent Compatibility Matrix?

Have you ever taken a look at Microsoft’s published compatibility matrix for SQL Server lock compatibility? If you have, then I don’t need to tell you that it sucks in a very big way.

Have no fear, I have painstakingly redrawn at better one (via PowerPoint no less!) and am making available to you totally free of charge as a lovely PDF slide here…Why you ask? Because I love you all!

Not too bad for version 1 eh!

If you have any comments for improvements OR find any errors then please don’t hesitate to contact me (you know how!).

Yours,

retracement

Posted in sql, SQLServerPedia Syndication, tuning | 6 Comments

When should you use NOLOCK?

Well the quick answer to that question is never. And there you have it, the quickest blog post in history (perhaps!).

…Except that I should really have said “When should you use READUNCOMMITTED isolation or hint?” and then the question takes a little more of a surprising turn. Let me first start and explain why I have categorically ruled against the use of NOLOCK. The reason, is that at some point in the dim and distant future Microsoft may decide to remove this hint from the product. If they could do it today, they probably would; the only thing that is really stopping them is the numerous lines of external T-SQL code that dirtys the floors of many Development teams around the world.

Of course technically READUNCOMMITTED and NOLOCK are identical with the exception that the former can also be used to set the session level ISOLATION LEVEL as well as the statement level.

Now, moving swiftly onto a surprising find by myself whilst I was putting together material and investigating ideas for my “READPAST & Furious” presentation, which is based loosely on the excellent presentations that Kalen Delaney has given over the last 6 years or more on the subject of Locking, Blocking and Concurrency. It is a homage of sorts. During her SQL PASS Summit 2010 Pre-Conference day “Locking and Blocking and Row Versions, Oh My!“, there was a question posed about whether there was ever a good reason to use the NOLOCK hint. The outcome was (quite unsurprising) ‘not really, maybe only for troubleshooting’.

Another thing that is commonly said about NOLOCK is that it does not take out Shared locks and this is the reason why when using the NOLOCK (or READUNCOMMITTED) hint, we are able to read uncommitted data. Since a Shared lock is not taken or requested, there is no incompatibility detected by the lock manager and therefore no waiting for Exclusive locks to be released from any dirty pages.

Now imagine my surprise when I was playing with several SELECT statements and came across locks being acquired when using READUNCOMMITTED (or NOLOCK dont forget!) hint. So if I perform a simple “SELECT * FROM orders WITH (READUNCOMMITTED) from an entire table we can see what locks (if any) are acquired…

Ooooh look Ma, Locks!!!

Now perhaps less of a surprise is the acquiring of a Sch-S lock (otherwise known as a Schema Stability lock) on the table itself. This is (obviously) being acquired in order to prevent any Sch-M locks being taken whilst the SELECT executes so that the table structure cannot be changed, and this happens with every query. In fact if you look at Books Online it evens warns you that this is what will happen :-

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. For more information about lock behavior, see Lock Compatibility (Database Engine).

Much more surprising is the acquiring of the Shared lock which is classed as a “[BULK_OPERATION]” in the TextData column. Now this one was totally unexpected and if we take a look at the Type column we see the value “12=HOBT” (Heap or B-Tree). This table my friends is in fact a Heap, which is only of slight relevance to this investigation but is worth remembering. So to summarize so far, using READUNCOMMITTED hint in our SELECT against a Heap causes a Schema Stability lock on table object AND a special [Bulk Operation] Shared lock on the Heap!

Next thing I am now going to do is run the SELECT statement under READ COMMITTED isolation. Doing so we get the following results and they are just as we expected, a single Intent Shared lock on the table object and multiple Shared locks acquired and released on each and every Page.

Normality

Now then, let us forget for a moment the “Uncommitted” argument when using READUNCOMMITTED isolation and just agree on one thing -locking was more efficient in the first example wasn’t it?! From a resource perspective, having to acquire and release all those page locks on a very large table would take it’s toll in a very high through put system.

Now the next thing I am going to do is run the SELECT * FROM orders statement in SERIALIZABLE isolation, and again no real surprises with what happens. You see that an Intent Shared lock and a Shared lock is acquired (and subsequently released) on the table.

Readings are normal Jim.

What I am going to do now is to create a clustered index on the table and repeat the SELECT using READUNCOMMITTED and the result (from a locking perspective) is even better as you can see below.

We only have stability

So this time the READUNCOMMITTED lock gives us an even more efficient turn around once the clustered index is in place, it simply takes out a single Schema Stability lock on the table. We go back then to the original question, “When should you use NOLOCK READUNCOMMITTED?” and I shall tell you! Whenever you are querying tables that are housed in read only databases, read only filegroups, read only file systems, Scalable Shared Databases (in short committed data that is read only) it makes perfect sense to use a locking strategy that uses no locking due to the following reasons:-

  1. Improves concurrency
  2. Reduces lock manager overhead
  3. Reduces memory usage of locks

And what of the dirty data you might ask? If it is read only there is no danger of it being changed and therefore no danger of lost updates or dirty reads. In other words (think of “The Matrix” here) …. “Do not try and bend the spoon isolation. That’s impossible. Instead… only try to realize the truth. There is no spoon isolation (required)“.

Posted in performance, scale-out, sql, SQLServerPedia Syndication | 2 Comments