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

Selectivity<br />

Indexes, particularly non-clustered indexes, are primarily beneficial in situations where there is a reasonably<br />

high level of selectivity within the index. By selectivity, I’m referring to the percentage of values in<br />

the column that are unique. The higher the percentage of unique values within a column, the higher the<br />

selectivity is said to be, and the greater the benefit of indexing.<br />

If you think back to our sections on non-clustered indexes — particularly the section on non-clustered<br />

indexes versus a clustered index — you will recall that the lookup in the non-clustered index is really<br />

only the beginning. You still need to make another loop through the clustered index in order to find the<br />

real data. Even with the non-clustered index on a heap, you still end up with multiple physically separate<br />

reads to perform.<br />

If one lookup in your non-clustered index is going to generate multiple additional lookups in a clustered<br />

index, then you are probably better off with the table scan. The exponential effect that’s possible here is<br />

actually quite amazing. Consider that the looping process created by the non-clustered index is not<br />

worth it if you don’t have somewhere in the area of 90–95 percent uniqueness in the indexed column.<br />

Clustered indexes are substantially less affected by this because, once you’re at the start of your range of<br />

data — unique or not — you’re there. There are no additional index pages to read. Still, more than likely,<br />

your clustered index has other things for which it could be put to greater use.<br />

One other exception to the rule of selectivity has to do with foreign keys. If your table has a column that<br />

is a foreign key, then in all likelihood, you’re going to benefit from having an index on that column.<br />

Why foreign keys and not other columns? Well, foreign keys are frequently the target of joins with<br />

the table they reference. Indexes, regardless of selectivity, can be very instrumental in join performance<br />

because they allow what is called a merge join. A merge join obtains a row from each table and compares<br />

them to see if they match the join criteria (what you’re joining on). Since there are indexes on the<br />

related columns in both tables, the seek for both rows is very fast.<br />

The point here is that selectivity is not everything, but it is a big issue to consider. If the column in<br />

question is not in a foreign key situation, then it is almost certainly the second only to the, “How often<br />

will this be used?” question in terms of issues you need to consider.<br />

Watching Costs: When Less Is More<br />

284<br />

Remember that while indexes speed up performance when reading data, they are actually very costly<br />

when modifying data. Indexes are not maintained by magic. Every time that you make a modification to<br />

your data, any indexes related to that data also need to be updated.<br />

When you insert a new row, a new entry must be made into every index on your table. Remember, too,<br />

that when you update a row, it is handled as a delete and insert — again, your indexes have to be<br />

updated. But wait! There’s more! (Feeling like a late-night infomercial here.) When you delete records,<br />

again, you must update all the indexes, too — not just the data. For every index that you create, you are<br />

creating one more block of entries that have to be updated.<br />

Notice, by the way, that I said entries plural — not just one. Remember that a B-Tree has multiple levels<br />

to it. Every time that you make a modification to the leaf level, there is a chance that a page split will

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

Saved successfully!

Ooh no, something went wrong!