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 9: <strong>SQL</strong> <strong>Server</strong> Storage and Index Structures<br />

the different row locations. If you change the structure of the index using DROP_EXISTING, the NCIs are<br />

rebuilt only once instead of twice. Furthermore, you cannot simply drop and re-create an index created by<br />

a constraint, for example, to implement a certain fillfactor. DROP_EXISTING is a workaround to this.<br />

STATISTICS_NORECOMPUTE<br />

By default, <strong>SQL</strong> <strong>Server</strong> attempts to automate the process of updating the statistics on your tables and<br />

indexes. By selecting the STATISTICS_NORECOMPUTE option, you are saying that you will take responsibility<br />

for the updating of the statistics. In order to turn this option off, you need to run the UPDATE STATISTICS<br />

command, but not use the NORECOMPUTE option.<br />

I strongly recommend against using this option. Why? Well, the statistics on your index are what the<br />

query optimizer uses to figure out just how helpful your index is going to be for a given query. The statistics<br />

on an index are changing constantly as the data in your table goes up and down in volume and as<br />

the specific values in a column change. When you combine these two facts, you should be able to see that<br />

not updating your statistics means that the query optimizer is going to be running your queries based on<br />

out-of-date information. Leaving the automatic statistics feature on means that the statistics will be updated<br />

regularly (just how often depends on the nature and frequency of your updates to the table). Conversely,<br />

turning automatic statistics off means that you will either be out of date, or you will need to set up a<br />

schedule to manually run the UPDATE STATISTICS command.<br />

SORT_IN_TEMPDB<br />

280<br />

Using this option makes sense only when tempdb is stored on a physically separate drive from the database<br />

that is to contain the new index. This is largely an administrative function, so I’m not going to<br />

linger on this topic for more than a brief overview of what it is and why it only makes sense when tempdb<br />

is on a separate physical device.<br />

When <strong>SQL</strong> <strong>Server</strong> builds an index, it has to perform multiple reads to take care of the various index construction<br />

steps:<br />

1. Read through all the data, constructing a leaf row corresponding to each row of actual data. Just<br />

like the actual data and final index, these go into pages for interim storage. These intermediate<br />

pages are not the final index pages, but rather a holding place to temporarily store things every<br />

time the sort buffers fill up.<br />

2. A separate run is made through these intermediate pages to merge them into the final leaf pages<br />

of the index.<br />

3. Non-leaf pages are built as the leaf pages are being populated.<br />

If the SORT_IN_TEMPDB option is not used, then the intermediate pages are written out to the same<br />

physical files that the database is stored in. This means that the reads of the actual data have to compete<br />

with the writes of the build process. The two cause the disk heads to move to different places (read vs.<br />

write). The result is that the disk heads are constantly moving back and forth — this takes time.<br />

If, on the other hand, SORT_IN_TEMPDB is used, then the intermediate pages will be written to tempdb<br />

rather than the database’s own file. If they are on separate physical drives, this means that there is no<br />

competition between the read and write operations of the index build. Keep in mind, however, that this

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

Saved successfully!

Ooh no, something went wrong!