Index seek and Index scan ....order of columns

  • Below is my query and i have two different NC indices. Now when i have the first index, below query is doing a cluster index scan and if i have the second index query is doing a seek. I have two questions.

    Does order of index matter on the columns which you are joining and using a where condition.Which column should come first , should i use the column where i am using '='operator or the one on which i am joining

    Create Index Test_NC1 On RcrdSt

    ( Rkey,

    Tid)

    Create Index Test_NC2 On RcrdSt

    ( Tid,Rkey)

    select Rkey,Tid from Rgrst

    inner RcrdSt RTS_P

    on R.ID = RTS_P.RKey

    AND RTS_P.TID = 11

  • Primary purpose of an index is to reduce the number of rows in consideration for a query as early as possible. Which reduces row count, a filter or a join?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (9/26/2013)


    Primary purpose of an index is to reduce the number of rows in consideration for a query as early as possible. Which reduces row count, a filter or a join?

    Makes perfect sense, filter gives less rows.

  • GilaMonster (9/26/2013)


    Primary purpose of an index is to reduce the number of rows in consideration for a query as early as possible. Which reduces row count, a filter or a join?

    Another question on the same line. I have a clustered index on two columns C1 accepts NULL and C2 doesn't. Now in my nc index when i dont include C2 it is still using the right index, but when i dont include c1 it is not using the right index. Is it because c1 has null values?

  • No, that won't be a cause.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • curious_sqldba (9/26/2013)


    Now in my nc index when i dont include C2 it is still using the right index, but when i dont include c1 it is not using the right index. Is it because c1 has null values?

    Because INdex helps when your query's filter/clause has LEFT most column of the index.

    Lets say : if index is on col1 and col2

    then col1 must be part of query then only index will be used.

    but if you ignore col1 and use ONLY col2 then this index will not get used.

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

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

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