Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server Audits: IntroductionSQL Advent 2011 Day 19: Filtered Indexes »
    comments

    In my Are you ready for SQL Server 2012 or are you still partying like it is 1999? post, I wrote about how you should start using SQL Server 2005 and SQL Server 2008 functionality now in order to prepare for SQL Server 2012. I still see tons of code that is written in the pre 2005 style and people still keep using those functions, procs and statements even though SQL Server 2005 and 2008 have much better functionality.

    Today we are going to take a look at Indexes With Included Columns
    When you use Indexes With Included Columns you can do two things that you cannot do with regular non clustered indexes
    1) You can go over the 900 byte limit
    2) You can use data types not allowed as index key columns

    By including nonkey columns, you can create nonclustered indexes that cover more queries. To learn more about covering indexes, take a look at SQL Server covering indexes written by George Mastros

    Let's get started to see what I mean, first create these two identical tables

    1. SELECT * INTO Test1 FROM sys.objects
    2.  
    3. SELECT * INTO Test2 FROM sys.objects

    Now create this index on the Test1 table

    1. CREATE INDEX ix_Test1 ON Test1(type,is_ms_shipped)

    Running a simple query reveals a seek

    1. SELECT type,is_ms_shipped
    2. FROM Test1
    3. WHERE type = 's'
    4. AND is_ms_shipped = 1



    Now let's add the name column to the columns returned or to the WHERE clause

    1. SELECT type,is_ms_shipped,name
    2. FROM Test1
    3. WHERE type = 's'
    4. AND is_ms_shipped = 1
    5.  
    6.  
    7. SELECT type,is_ms_shipped
    8. FROM Test1
    9. WHERE type = 's'
    10. AND is_ms_shipped = 1
    11. AND name LIKE 's%'



    As you can see, we get a scan now, this is because the index is not covering the query

    Create the following index on the Test2 table, as you can see the only difference bwteween the index on the Test1 table is that it has the name as an included column

    1. CREATE INDEX ix_Test2 ON Test2(type,is_ms_shipped) INCLUDE (name)

    Running the first query where the name is in the WHERE clause against the Test2 and the Test1 table shows much better performance for the index with the included column, this is of course expected

    1. SELECT type,is_ms_shipped
    2. FROM Test2
    3. WHERE type = 's'
    4. AND is_ms_shipped = 1
    5. AND name LIKE 's%'
    6.  
    7. SELECT type,is_ms_shipped
    8. FROM Test1
    9. WHERE type = 's'
    10. AND is_ms_shipped = 1
    11. AND name LIKE 's%'



    As you can see, we get an index seek now for the table with the index that has included columns

    Running the second query where the name is in the WHERE clause as well as returned in the results against the Test2 and the Test1 table shows much better performance for the index with the included column, this is of course expected

    1. SELECT type,is_ms_shipped,name
    2. FROM Test2
    3. WHERE type = 's'
    4. AND is_ms_shipped = 1
    5. AND name LIKE 's%'
    6.  
    7.  
    8.  
    9. SELECT type,is_ms_shipped,name
    10. FROM Test1
    11. WHERE type = 's'
    12. AND is_ms_shipped = 1
    13. AND name LIKE 's%'

    And we get a seek as well for the table with the index that has included columns

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    767 views
    Instapaper

    1 comment

    Comment from: kermit [Member] Email
    Thanks Denis, this article proved useful for a colleague at work :D
    02/09/12 @ 04:14

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)