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

Sparse columns largely fall under the heading of “advanced topic,” but I do want you to know they are<br />

there and can be a viable solution for particular scenarios.<br />

Understanding Indexes<br />

264<br />

Webster’s dictionary defines an index as:<br />

A list (as of bibliographical information or citations to a body of literature) arranged usually in alphabetical<br />

order of some specified datum (as author, subject, or keyword).<br />

I’ll take a simpler approach in the context of databases and say it’s a way of potentially getting to data a<br />

heck of a lot quicker. Still, the Webster’s definition isn’t too bad — even for our specific purposes.<br />

Perhaps the key thing to point out in the Webster’s definition is the word “usually” that’s in there. The<br />

definition of “alphabetical order” changes depending on a number of rules. For example, in <strong>SQL</strong> <strong>Server</strong>,<br />

we have a number of different collation options available to us. Among these options are:<br />

❑ Binary — Sorts by the numeric representation of the character (for example, in ASCII, a space is<br />

represented by the number 32, the letter “D” is 68, and the letter “d” is 100). Because everything<br />

is numeric, this is the fastest option. Unfortunately, it’s not at all the way in which people think,<br />

and can also really wreak havoc with comparisons in your WHERE clause.<br />

❑ Dictionary order — This sorts things just as you would expect to see in a dictionary, with a<br />

twist. You can set a number of different additional options to determine sensitivity to case,<br />

accent, and character set.<br />

It’s fairly easy to understand that if we tell <strong>SQL</strong> <strong>Server</strong> to pay attention to case, then “A” is not going to<br />

be equal to “a.” Likewise, if we tell it to be case insensitive, then “A” will be equal to “a.” Things get a<br />

bit more confusing when you add accent sensitivity. <strong>SQL</strong> <strong>Server</strong> pays attention to diacritical marks, and<br />

therefore “a” is different from “á,” which is different from “à.” Where many people get even more confused<br />

is in how collation order affects not only the equality of data, but also the sort order (and, therefore,<br />

the way it is stored in indexes).<br />

By way of example, let’s look at the equality of a couple of collation options in the following table, and<br />

what they do to our sort order and equality information:<br />

Collation Order Comparison Values Index Storage Order<br />

Dictionary order, case insensitive,<br />

accent insensitive (the default)<br />

Dictionary order, case insensitive,<br />

accent insensitive, uppercase<br />

preference<br />

Dictionary order, case sensitive A ≠ a, Ä ≠ ä, Å ≠ å,<br />

a ≠ à ≠ á ≠ â ≠ ä ≠ å,<br />

A ≠ Ä ≠ Å<br />

A = a = à = á = â = Ä = ä = Å = å a, A, à, â, á, Ä, ä, Å, åt<br />

A = a = à = á = â = Ä = ä = Å = å A, a, à, â, á, Ä, ä, Å, å<br />

A, a, à, á, â, Ä, ä, Å, å

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

Saved successfully!

Ooh no, something went wrong!