Can composite indexes be used as an index on a single field?

  • I've got a table that has a composite index on three of its fields. I don't want to create another index if I don't have to, but I'm trying to speed up some views that are taking a while.

    I'm doing a rollup on PartNumber, which is the first field in the composite index.

    Will it use that index to accomplish a search on PartNumber alone if needed? Will it do it for any field in the composite index?

  • It should use part number if you search for part number alone. I'm not sure about the rest. You could test by viewing the execution plans to be certain.

  • since you said the PartNumber is the first field, then yes; but it also depends on how selective that single column is.

    if there's a lot of common values, SQL might just decide to do a clustered index scan instead.

    so to confirm, if PartNumber is the leading column in your index that has three fields, would it be posible for a query to use the index.

    so if the index is (PartNumber,SecondKey,ThirdKey) then yes.

    if the index was created as (FirstKey,SecondKey,PartNumber) then no, you'd need an extra idnex.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • It's better to test by viewing the execution plans.

    --

    SQLBuddy

  • It goes back to the histogram on the statistics. That is only build on the first column in the index key. The others are used to help establish the density (how selective the entire key is), but the histogram is only on that first one.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply