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

274<br />

At the leaf level, we have a rather sharp difference from what we’ve seen with the other two index structures:<br />

We have yet another index to look over. With clustered indexes, when we got to the leaf level, we<br />

found the actual data. With non-clustered indexes on a heap, we didn’t find the actual data, but did find<br />

an identifier that let us go right to the data (we were just one step away). With non-clustered indexes on<br />

a clustered table, we find the cluster key. That is, we find enough information to go and make use of the<br />

clustered index.<br />

We end up with something that looks like Figure 9-7.<br />

What we end up with is two entirely different kinds of lookups.<br />

In the example from our diagram, we start off with a ranged search. We do one single lookup in our<br />

index and are able to look through the non-clustered index to find a continuous range of data that meets<br />

our criterion (LIKE ‘T%’). This kind of lookup, where we can go right to a particular spot in the index,<br />

is called a seek.<br />

The second kind of lookup then starts — the lookup using the clustered index. This second lookup is<br />

very fast; the problem lies in the fact that it must happen multiple times. You see, <strong>SQL</strong> <strong>Server</strong> retrieved a<br />

list from the first index lookup (a list of all the names that start with “T”), but that list doesn’t logically<br />

match up with the cluster key in any continuous fashion. Each record needs to be looked up individually,<br />

as shown in Figure 9-8.<br />

Needless to say, this multiple-lookup situation introduces more overhead than if we had just been able to<br />

use the clustered index from the beginning. The first index search — the one through our non-clustered<br />

index — requires very few logical reads.<br />

For example, if I have a table with 1,000 bytes per row and I do a lookup similar to the one in our drawing<br />

(say, something that would return five or six rows); it would only take something to the order of<br />

8–10 logical reads to get the information from the non-clustered index. However, that only gets me as far<br />

as being ready to look up the rows in the clustered index. Those lookups would cost approximately 3–4<br />

logical reads each, or 15–24 additional reads. That probably doesn’t seem like that big a deal at first, but<br />

look at it this way:<br />

Logical reads went from 3 minimum to 24 maximum — that’s an 800 percent increase in the amount of<br />

work that had to be done.<br />

Now expand this thought out to something where the range of values from the non-clustered index wasn’t<br />

just five or six rows, but five or six thousand, or five or six hundred thousand rows — that’s going to be a<br />

huge impact.<br />

Don’t let the extra overhead vs. a clustered index scare you — the point isn’t meant to scare you away<br />

from using indexes, but rather to point out that a non-clustered index is generally not going to be as<br />

efficient as a clustered index from a read perspective (it can, in some instances, actually be a better<br />

choice at insertion time). An index of any kind is usually (there are exceptions) the fastest way to do a<br />

lookup. We’ll explain what index to use and why later in the chapter.

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

Saved successfully!

Ooh no, something went wrong!