SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
curious_sqldba
curious_sqldba
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7778 Visits: 3696
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



GilaMonster
GilaMonster
SSC Guru
SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)

Group: General Forum Members
Points: 234057 Visits: 46366
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


curious_sqldba
curious_sqldba
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7778 Visits: 3696
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.
curious_sqldba
curious_sqldba
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7778 Visits: 3696
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?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)SSC Guru (234K reputation)

Group: General Forum Members
Points: 234057 Visits: 46366
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


Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13850 Visits: 4077
curious_sqldba (9/26/2013)
[quote][b]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;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search