Index Precedence

  • I'm looking to tune up some queries that are taking forever to run for a report and the thing that I'm noticing is that in my execution plan the incorrect index for a table is being used. I'm joining on the PK to another table but am getting an 'Index Scan' versus 'Seek'. The PK is non-clustered (don't ask why) so by default the SQL engine is picking the clustered index. Here's my query along w/ the execution plan an indexes...

    Am I correct in assuming SQL chooses the clustered index by default or would it be some sort of 'precedence order' that can be specified somewhere?

    SELECT

    'x'

    FROM

    MyDB1..table1 a

    INNER JOIN MyDB2..table2 b ON

    a.SubsidiaryNo = b.SubsidiaryNo

    Table1

    Index_NameDescriptionKeys
    IX_Table1_AccountNo_RepId_AcctClass_PrimaryAlphaKey1nonclustered located on PRIMARYAccountNo, RepId, AccountClass, PrimaryAlphaKey1
    IX_Table1_AccountNo_StateCodenonclustered located on PRIMARYAccountNo, StateCode
    IX_Table1_AsOfDatenonclustered located on PRIMARYAsOfDate
    IX_Table1_MMFSecurityNononclustered located on PRIMARYMMFSecurityNo
    IX_Table1_OldAccountNononclustered located on PRIMARYOldAccountNo
    IX_Table1_RepId_AcctClassclustered located on PRIMARYRepId, AccountClass
    IX_Table1_RepIdBeforeACATsnonclustered located on PRIMARYRepIdBeforeACATS
    IX_Table1_SSNTINnonclustered located on PRIMARYSSNTIN
    PK_Table1_OFFLINEnonclustered, unique, primary key located on PRIMARYAccountNo

    Table 2

    Index_NameDescriptionKeys
    PK__SubsidiaryCorrBD__56757D0Dclustered, unique, primary key located on PRIMARYSubsidiaryNo

    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • So I guess the real question is...how does SQL Server choose which index to use?

    Anyone...?


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • SELECT

    'x'

    FROM

    MyDB1..table1 a

    INNER JOIN MyDB2..table2 b ON

    a.SubsidiaryNo = b.SubsidiaryNo

    is your query, but I don't see a.SubsidiaryNo in any of the indexes for table 1.  This may be why it is doing a scan of the clustered index (it also happens to be where the data resides and it has to look at the data to compare a.SubsidiaryNo to b.SubsidiaryNo.  I'd build an index on a.SubsidiaryNo.

  • Theoretically, under the assumption that the statistics are recently updated (and indexes are not fragmented), SQL server chooses one index over another because choosing that one would result in a better execution plan and thus better performance.

    If, however, for whatever reason, you do not agree with the choice, you can force(hint) SQL Server to use a specific index of your choice.

    Check this out: http://www.sqlservercentral.com/columnists/rmarda/performanceaddinghints_printversion.asp

  • Ah okay so it's based on table statistics and index fragmentation...yeah I'm including table hints in my queries here because for some reason the clustered index on my table1 is on a field that is not referenced often (to the best of my knowledge) I think I will work w/ our DBA's to find out whats up w/ that and see if it can be changed...


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • Also, presuming 'x', is the only field you're returning, add that field to an index on SubsidairyID, non-clustered or clustered. SQL will prefer that index as it will be able to find and return the data within the same index. It doesn't need to do any bookmark lookups to return additional information.

    --------------------
    Colt 45 - the original point and click interface

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

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