14.04.2013 Views

SQL SERVER 2008

Create successful ePaper yourself

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

Locks ❘ 167<br />

You may look at this table with a degree of hope that your locks never end up too far down the list.<br />

It’s quite understandable and reasonable to expect that your normal querying behavior should be<br />

able to get away with just locking rows, pages, and occasionally a whole HoBT; but remember that<br />

a single object’s locks can cover many HoBT locks, which in turn, might cover thousands or millions<br />

of pages, and who knows how many rows. A trade-off must be made between having a smaller<br />

number of locks with more data locked than strictly necessary and having less data locked with a<br />

larger number of locks.<br />

Lock escalation occurs when a number of locks are converted into a smaller number of locks at<br />

levels further down that list (typically to the object level) — that is, making the trade-off to reduce<br />

the number of locks through coarser granularity. This can be benefi cial in that it reduces the amount<br />

of overhead to manage the locks; but of course with more data locked, there is a higher likelihood of<br />

processes being blocked by encountering locked data. Details about how this escalation occurs are<br />

covered later in the chapter, after the lock modes and compatibility between the lock modes have<br />

been considered. For now, be aware of the kinds of things that can be locked.<br />

Lock Modes<br />

Data in a database is not like a book, which can only be in the possession of one person at a time. If<br />

you are reading a book, the book is in your hands and other people can’t read it. Data is more like a<br />

notice on a board. You and other people can read it at the same time. However, if you want to change<br />

it, then you need to take the notice down off the board, and no one else can change it at the same<br />

time. Whether or not they can read it while it is being changed is a separate matter (the isolation<br />

level), but this scenario is related to the concept of lock modes, and the compatibility matrix between<br />

them, as described in the following sections.<br />

Shared Lock Mode (S)<br />

When a read request for a row of data is made by a task, by default, <strong>SQL</strong> Server will request a lock<br />

in shared mode. Shared mode is compatible with most other locks, as it is only permitted to read the<br />

row on the data page.<br />

Update Lock Mode (U)<br />

Update mode is a special kind of lock. It is used when searching data during a data modifi cation<br />

request. The process is straightforward: <strong>SQL</strong> Server uses the update lock by locating the data<br />

and then preventing others from updating it. It prevents other requests from modifying the data<br />

by virtue of the update lock’s compatibility with other locks. Any other requests wishing to lock<br />

the resource with an update or exclusive lock are forced to wait. However, in order to effect the<br />

data modifi cation, the update lock must be converted to an exclusive lock. As the update lock has<br />

blocked all other data modifi cation locks, all it needs to do is wait until it can get an exclusive lock<br />

when the last, if any, shared locks have been released. This allows for greater concurrency in the<br />

system as opposed to all writers just taking exclusive locks. If the latter were the case, then blocking<br />

would be a much greater problem. Concurrent queries would be blocked for the entire duration of<br />

the update (the read part and the write) as opposed to just the write.

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

Saved successfully!

Ooh no, something went wrong!