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.

You may hear lots of bad things about table scans, and in general, they will be true. However, table scans<br />

can actually be the fastest method of access in some instances. Typically, this is the case when retrieving<br />

data from rather small tables. The exact size where this becomes the case will vary widely according to<br />

the width of your table and the specific nature of the query.<br />

See if you can spot why the use of EXISTS in the WHERE clause of your queries has so much to offer<br />

performance-wise when it fits the problem. When you use the EXISTS operator, <strong>SQL</strong> <strong>Server</strong> stops as<br />

soon as it finds one record that matches the criteria. If you had a million-record table and it found a<br />

matching record on the third record, then use of the EXISTS option would have saved you the reading<br />

of 999,997 records! NOT EXISTS works in much the same way.<br />

Use of Indexes<br />

When <strong>SQL</strong> <strong>Server</strong> decides to use an index, the process actually works somewhat similarly to a table scan,<br />

but with a few shortcuts.<br />

During the query optimization process, the optimizer takes a look at all the available indexes and<br />

chooses the best one (this is primarily based on the information you specify in your joins and WHERE<br />

clause, combined with statistical information <strong>SQL</strong> <strong>Server</strong> keeps on index makeup). Once that index is<br />

chosen, <strong>SQL</strong> <strong>Server</strong> navigates the tree structure to the point of data that matches your criteria and again<br />

extracts only the records it needs. The difference is that since the data is sorted, the query engine knows<br />

when it has reached the end of the current range it is looking for. It can then end the query, or move on<br />

to the next range of data as necessary.<br />

If you ponder the query topics we’ve studied thus far (Chapter 7 specifically), you may notice some<br />

striking resemblances to how the EXISTS option works. The EXISTS keyword allowed a query to quit<br />

running the instant that it found a match. The performance gains using an index are similar or better<br />

than EXISTS since the process of searching for data can work in a similar fashion; that is, the server can<br />

use the sort of the index to know when there is nothing left that’s relevant and can stop things right<br />

there. Even better, however, is that by using an index, we don’t have to limit ourselves to Boolean situations<br />

(does the piece of data I was after exist — yes or no?). We can apply this same notion to both the<br />

beginning and end of a range. We are able to gather ranges of data with essentially the same benefits that<br />

using an index gives to finding data. What’s more, we can do a very fast lookup (called a SEEK) of our<br />

data rather than hunting through the entire table.<br />

Don’t get the impression from my comparing what indexes do to what the EXISTS operator does that<br />

indexes replace the EXISTS operator altogether (or vice versa). The two are not mutually exclusive; they<br />

can be used together, and often are. I mention them here together only because they have the similarity of<br />

being able to tell when their work is done, and quit before getting to the physical end of the table.<br />

Index Types and Index Navigation<br />

Although there are nominally two types of base index structures in <strong>SQL</strong> <strong>Server</strong> (clustered and non-clustered),<br />

there are actually, internally speaking, three different types:<br />

❑ Clustered indexes<br />

Chapter 9: <strong>SQL</strong> <strong>Server</strong> Storage and Index Structures<br />

❑ Non-clustered indexes, which comprise:<br />

❑ Non-clustered indexes on a heap<br />

❑ Non-clustered indexes on a clustered index<br />

269

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

Saved successfully!

Ooh no, something went wrong!