• gosh (11/24/2010)


    I have to admit that I am not impressed with the title. It is wrong. According to your article, the column order in a composite index is not important.

    Go back over the article and comments. The order in the index is important and takes some knowledge on which columns to include in each index.

    With an index on (Lastname, Firstname), as long as you are searching on Lastname and Firstname in your WHERE clause, the optimizer is smart enough to figure out the right order to search with to use the (Lastname, Firstname) index. However, only searching on Firstname will cause the optimizer to choose either a table/index scan or use a different index.

    Visualize the index as this:

    Lastname, Firstname

    Ang, Joe

    Angelou, Mitch

    Henry, Ben

    Selby, Josh

    Zenith, Sharp

    If you are searching for only Firstname, how would this index be helpful? You would have to scan the whole index. And if you have to scan the whole index, you might as well scan the table. But if you search on only LastName, the index is still useful, even though you're not looking for Firstname. And it's even better if you search for Lastname but want to return Firstname too because it's part of the index so there isn't a bookmark lookup on the table.

    One of the keys in designing an index is to know how the data will be searched. If you always search on Lastname and sometimes Lastname + Firstname, then the index (Lastname, Firstname) will serve you well. If you sometimes need to search on Firstname but still plan on the previous statement being true, then create an index on (Firstname) INCLUDE (Lastname).

    /* Anything is possible but is it worth it? */