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

A question that might come to mind is why ascending vs. descending matters — <strong>SQL</strong> <strong>Server</strong> can just<br />

look at an index backwards if it needs the reverse sort order. Life is not, however, always quite so simple.<br />

Looking at the index in reverse order works just fine if you’re dealing with only one column, or if your<br />

sort is always the same for all columns, but what if you needed to mix sort orders within an index? That<br />

is, what if you need one column to be sorted ascending, but the other descending? Since the indexed<br />

columns are stored together, reversing the way you look at the index for one column would also reverse<br />

the order for the additional columns. If you explicitly state that one column is ascending and the other is<br />

descending, then you invert the second column right within the physical data — there is suddenly no<br />

reason to change the way that you access your data.<br />

As a quick example, imagine a reporting scenario where you want to order your employee list by the<br />

hire date, beginning with the most recent (a descending order), but you also want to order by their last<br />

name (an ascending order). Before indexes were available, <strong>SQL</strong> <strong>Server</strong> would have to do two operations<br />

— one for the first column and one for the second. Having control over the physical sort order of<br />

our data provides flexibility in the way we can combine columns.<br />

INCLUDE<br />

This one is supported with <strong>SQL</strong> <strong>Server</strong> 2005 and later. Its purpose is to provide better support for what<br />

are called covered queries.<br />

When you INCLUDE columns as opposed to placing them in the ON list, <strong>SQL</strong> <strong>Server</strong> only adds them at the<br />

leaf level of the index. Because each row at the leaf level of an index corresponds to a data row, what<br />

you’re doing is essentially including more of the raw data in the leaf level of your index. If you think<br />

about this, you can probably make the guess that INCLUDE really applies only to non-clustered indexes<br />

(clustered indexes already are the data at the leaf level, so there would be no point).<br />

Why does this matter? Well, as we’ll discuss further as the book goes on, <strong>SQL</strong> <strong>Server</strong> stops working as<br />

soon as it has what it actually needs. So if while traversing the index it can find all the data that it needs<br />

without continuing to the actual data row, then it won’t bother going to the data row (what would be the<br />

point?). By including a particular column in the index, you may “cover” a query that utilizes that particular<br />

index at the leaf level and save the I/O associated with using that index pointer to go to the data page.<br />

WITH<br />

Careful not to abuse this one! When you include columns, you are enlarging the size of the leaf level of<br />

your index pages. That means fewer rows will fit per page, and therefore, more I/O may be required to<br />

see the same number of rows. The result may be that your effort to speed up one query may slow down<br />

others. To quote an old film from the eighties, “Balance, Danielson — balance!” Think about the effects<br />

on all parts of your system, not just the particular query you’re working on that moment.<br />

WITH is an easy one; it just tells <strong>SQL</strong> <strong>Server</strong> that you will indeed be supplying one or more of the options<br />

that follow.<br />

PAD_INDEX<br />

278<br />

In the syntax list, this one comes first — but that will seem odd when you understand what PAD_INDEX<br />

does. In short, it determines just how full the non-leaf level pages of your index are going to be (as a percentage)<br />

when the index is first created. You don’t state a percentage on PAD_INDEX because it will use<br />

whatever percentage you specify in the FILLFACTOR option that follows. Setting PAD_INDEX = ON<br />

would be meaningless without a FILLFACTOR (which is why it seems odd that it comes first).

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

Saved successfully!

Ooh no, something went wrong!