17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14: Transactions and Locks<br />

Lock Escalation and Lock Effects on Performance<br />

Escalation is all about recognizing that maintaining a finer level of granularity (say, a row lock instead of<br />

a page lock) makes a lot of sense when the number of items being locked is small. However, as we get<br />

more and more items locked, the overhead associated with maintaining those locks actually hinders performance.<br />

It can cause the lock to be in place longer (thus creating contention issues — the longer the<br />

lock is in place, the more likely that someone will want that particular record). When you think about<br />

this for a bit, you’ll realize there’s probably a balancing act to be done somewhere, and that’s exactly<br />

what the lock manager uses escalation to do.<br />

When the number of locks being maintained reaches a certain threshold, the lock is escalated to the next<br />

highest level and the lower-level locks do not have to be so tightly managed (freeing resources and helping<br />

speed over contention).<br />

Note that the escalation is based on the number of locks rather than the number of users. The importance<br />

here is that you can single-handedly lock a table by performing a mass update — a row lock can<br />

graduate to a page lock, which then escalates to a table lock. That means that you could potentially be<br />

locking every other user out of the table. If your query makes use of multiple tables, it’s actually quite<br />

possible to wind up locking everyone out of all of those tables.<br />

While you certainly would prefer not to lock all the other users out of your object, there are times when<br />

you still need to perform updates that are going to have that effect. There is very little you can do about<br />

escalation other than to keep your queries as targeted as possible. Recognize that escalations will happen,<br />

so make sure you’ve thought about what the possible ramifications of your query are.<br />

Lock Modes<br />

Beyond considering just what resource level you’re locking, you also should consider what lock mode<br />

your query is going to acquire. Just as there are a variety of resources to lock, there are also a variety of<br />

lock modes.<br />

Some modes are exclusive of each other (which means they don’t work together). Some modes do nothing<br />

more than essentially modify other modes. Whether modes can work together is based on whether<br />

they are compatible (we’ll take a closer look at compatibility between locks later in this chapter).<br />

Just as we did with lockable resources, let’s take a look at lock modes one by one.<br />

Shared Locks<br />

438<br />

This is the most basic type of lock there is. A shared lock is used when you need only to read the data —<br />

that is, you won’t be changing anything. A shared lock wants to be your friend, as it is compatible with<br />

other shared locks. That doesn’t mean that it still won’t cause you grief — while a shared lock doesn’t<br />

mind any other kind of lock, there are other locks that don’t like shared locks.<br />

Shared locks tell other locks that you’re out there. It’s the old, “Look at me! Ain’t I special?” thing. They<br />

don’t serve much of a purpose, yet they can’t really be ignored. However, one thing that shared locks do<br />

is prevent users from performing dirty reads.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!