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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The syntax for switching between them is pretty straightforward:<br />

SET TRANSACTION ISOLATION LEVEL <br />

The change in isolation level will affect only the current connection, so you don’t need to worry about<br />

adversely affecting other users (or them affecting you).<br />

Let’s start by looking at the default situation (READ COMMITTED) a little more closely.<br />

READ COMMITTED<br />

With READ COMMITTED, any shared locks you create will be automatically released as soon as the statement<br />

that created them is complete. That is, if you start a transaction, run several statements, run a<br />

SELECT statement, and then run several more statements, the locks associated with the SELECT statement<br />

are freed as soon as the SELECT statement is complete — <strong>SQL</strong> <strong>Server</strong> doesn’t wait for the end of the<br />

transaction.<br />

Action queries (UPDATE, DELETE, and INSERT) are a little different. If your transaction performs a query<br />

that modifies data, then those locks will be held for the duration of the transaction (in case you need to<br />

roll back).<br />

By keeping this level of default with READ COMMITTED, you can be sure that you have enough data<br />

integrity to prevent dirty reads. However, non-repeatable reads and phantoms can still occur.<br />

READ UNCOMMITTED<br />

Chapter 14: Transactions and Locks<br />

READ UNCOMMITTED is the most dangerous of all isolation level choices, but also has the highest performance<br />

in terms of speed.<br />

Setting the isolation level to READ UNCOMMITTED tells <strong>SQL</strong> <strong>Server</strong> not to set any locks and not to honor<br />

any locks. With this isolation level, it is possible to experience any of the various concurrency issues we<br />

discussed earlier in the chapter (most notably a dirty read).<br />

Why would one ever want to risk a dirty read? When I watch the newsgroups on Usenet, I see the question<br />

come up on a regular basis. It’s surprising to a fair number of people, but there are actually good<br />

reasons to have this isolation level, and they almost always have to do with reporting.<br />

In an OLTP environment, locks are both your protector and your enemy. They prevent data integrity<br />

problems, but they also often prevent or block you from getting at the data you want. It is extremely<br />

commonplace to see a situation where the management wants to run reports regularly, but the data<br />

entry people are often prevented from or delayed in entering data because of locks held by the manager’s<br />

reports.<br />

By using READ UNCOMMITTED, you can often get around this problem — at least for reports where the<br />

numbers don’t have to be exact. For example, let’s say that a sales manager wants to know just how<br />

much has been done in sales so far today. Indeed, we’ll say he’s a micro-manager and asks this same<br />

question (in the form of rerunning the report) several times a day.<br />

443

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

Saved successfully!

Ooh no, something went wrong!