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.

Chapter 19: Playing Administrator<br />

index, where every page in both the leaf and non-leaf levels of the index have been reconstructed as you<br />

have defined them, either the defaults, or using switches to change things like the fill factor.<br />

Careful on this one. By default, as soon as you kick off a REBUILD, the index you are working on is<br />

essentially gone until the rebuild is complete. Any queries that relied on that index may become exceptionally<br />

slow (potentially by orders of magnitude). This is the sort of thing you want to test on an<br />

offline system first to have an idea how long it’s going to take, and then schedule to run in off hours,<br />

preferably with someone monitoring it to be sure it’s back online when peak hours come along.<br />

The Enterprise version of <strong>SQL</strong> <strong>Server</strong> does include a special ONLINE version of this option that will keep<br />

the index alive and build the new one parallel to the old, but realize that non-Enterprise versions are not<br />

going to have that option available, and, even if it is, the creation of the index is going to be a significant<br />

load on the system; tread carefully when rebuilding. In general, treat this one as though it can have<br />

major side effects while it runs, and leave its use squarely in the domain of the database administrator.<br />

DISABLE<br />

This one does what it says, only in somewhat drastic fashion. It would be nice if all this command did<br />

was take your index offline until you decided what you want to do, but instead it essentially marks the<br />

index as unusable. Once an index has been disabled, it must be rebuilt (not reorganized, but rebuilt)<br />

before it will be active again.<br />

This is one you’re very, very rarely going to do yourself. (You would more likely just drop the index.) It<br />

is far more likely to happen during a <strong>SQL</strong> <strong>Server</strong> upgrade or some other oddball situation.<br />

Yet another BE CAREFUL!!! warning on this one. If you disable the clustered index for your table, it<br />

has the effect of disabling the table. The data will remain, but will be inaccessible by all indexes, since<br />

they all depend on the clustered index — the entire table is effectively offline until you rebuild the clustered<br />

index.<br />

REORGANIZE<br />

BINGO!!! from the developer perspective. With REORGANIZE we hit a happy medium in life. When you<br />

reorganize your index, you get a slightly less complete optimization than you get with a full rebuild, but<br />

one that occurs online. (Users can still utilize the index.)<br />

This should, if you’re paying attention, bring about the question “What exactly do you mean by ‘slightly<br />

less complete’?” Well, REORGANIZE works only on the leaf level of your index; non-leaf levels of the<br />

index go untouched. This means that we’re not quite getting a full optimization, but, for the lion’s share<br />

of indexes, that is not where your real cost of fragmentation is, although it can happen and your mileage<br />

may vary.<br />

Given its much lower impact on users, this is usually the tool you’ll want to use as part of your regular<br />

maintenance plan; let’s take a look at running a index reorganization command.<br />

Try It Out Index Reorganization<br />

582<br />

To run this through its paces, we’re going to do a reorg on a table in the AdventureWorks<strong>2008</strong> database.<br />

The Production.TransactionHistory table is an excellent example of a table that is likely to have

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

Saved successfully!

Ooh no, something went wrong!