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 />

The syntax is simple enough:<br />

SAVE TRAN[SACTION] [| ]<br />

The thing to remember about save points is that they are cleared on ROLLBACK — that is, even if you<br />

save five save points, once you perform one ROLLBACK they are all gone. You can start setting new save<br />

points again and rolling back to those, but whatever save points you had when the ROLLBACK was<br />

issued are gone.<br />

SAVE TRAN can get extremely confusing and I can’t recommend it for the beginning user, but keep it in<br />

mind as being there.<br />

How the <strong>SQL</strong> Ser ver Log Works<br />

430<br />

You definitely must have the concept of transactions down before you try to figure out how <strong>SQL</strong> <strong>Server</strong><br />

tracks what’s what in your database. You see, what you think of as your database is only rarely a complete<br />

version of all the data. Except for rare moments when it happens that everything has been written to disk,<br />

the data in your database is made up of not only the data in the physical database file(s), but also any<br />

transactions that have been committed to the log since the last checkpoint.<br />

In the normal operation of your database, most activities that you perform are logged to the transaction<br />

log, rather than written directly to the database. A checkpoint is a periodic operation that forces all dirty<br />

pages for the database currently in use to be written to disk. Dirty pages are log or data pages that have<br />

been modified after they were read into the cache, but the modifications have not yet been written to<br />

disk. Without a checkpoint the log would fill up and/or use all the available disk space. The process works<br />

something like the diagram in Figure 14-1.<br />

Don’t mistake all this as meaning that you have to do something special to get your data out of the<br />

cache. <strong>SQL</strong> <strong>Server</strong> handles all of this for you. This information is only provided here to facilitate your<br />

understanding of how the log works and, from there, the steps required to handle a transaction. Whether<br />

something is in cache or not can make a big difference to performance, so understanding when things<br />

are logged and when things go in and out of the cache can be a big deal when you are seeking maximum<br />

performance.<br />

Note that the need to read data into a cache that is already full is not the only reason that a checkpoint<br />

would be issued. Checkpoints can be issued under the following circumstances:<br />

❑ By a manual statement (using the CHECKPOINT command)<br />

❑ At normal shutdown of the server (unless the WITH NOWAIT option is used)<br />

❑ When you change any database option (for example, single user only, dbo only, and so on)<br />

❑ When the Simple Recovery option is used and the log becomes 70 percent full<br />

❑ When the amount of data in the log since the last checkpoint (often called the active portion of<br />

the log) exceeds the size that the server could recover in the amount of time specified in the<br />

recovery interval option

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

Saved successfully!

Ooh no, something went wrong!