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


Index question


Index question

Author
Message
Sean Grebey
Sean Grebey
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 87
I am trying to speed up a Query joining two tables. For simplicity sake, we'll just put it as I am doing:

An Inner Join On Table1.FieldA and Table2.FieldA
A Where Clause on Table2.FieldB

Right now:
Table1 Has an Index on FieldA
Table2 has a Index on FieldA/FieldB
Table2 has a primary key ID field

Would putting an Index on Table2.FieldB by itself help at all since it is in the WHERE clause by itself?
Jared Karney
Jared Karney
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4976 Visits: 3694
I would answer this for you, but it is easy enough to set up and test. Look at the execution plans both before and after the index...

Thanks,
Jared
PFE - Microsoft
SQL Know-It-All
How to post data/code on a forum to get the best help - Jeff Moden
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87073 Visits: 45267
Maybe take a read through these:

http://www.sqlservercentral.com/articles/Indexing/68439/
http://www.sqlservercentral.com/articles/Indexing/68563/
http://www.sqlservercentral.com/articles/Indexing/68636/

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


Sean Grebey
Sean Grebey
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 87
Well anybody actually know the answer and could spare the 5 seconds to answer instead of wanting me to spend more time than I have to this holiday away from my family trying to grow as a person trying to figure myself it out myself, I'd much appreciate it.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87073 Visits: 45267
Sean Grebey (12/26/2012)
Would putting an Index on Table2.FieldB by itself help at all since it is in the WHERE clause by itself?


Maybe, depends what's in the select clause, how selective the filter is, how selective (if at all) the join is, what exactly the filter in the where clause is and a couple other factors.

Hence why I referred you to a set of articles (that I wrote), because there's no enough information in your post to answer the question and writing out all the various considerations and possibilities will take the entire rest of my holiday and probably end up as long as one of those articles.

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


Sean Grebey
Sean Grebey
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 87
GilaMonster (12/26/2012)
Sean Grebey (12/26/2012)
Would putting an Index on Table2.FieldB by itself help at all since it is in the WHERE clause by itself?


Maybe, depends what's in the select clause, how selective the filter is, how selective (if at all) the join is, what exactly the filter in the where clause is and a couple other factors.

Hence why I referred you to a set of articles (that I wrote), because there's no enough information in your post to answer the question and writing out all the various considerations and possibilities will take the entire rest of my holiday and probably end up as long as one of those articles.


Thanks for the links.
Jared Karney
Jared Karney
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4976 Visits: 3694
If it is as simple as what you provided... creating an index on Table2.FieldB will "probably" help. It will get rid of the scan and use a key lookup to get the data for the results. Again, "probably" if it is as simple as you have shown.

Thanks,
Jared
PFE - Microsoft
SQL Know-It-All
How to post data/code on a forum to get the best help - Jeff Moden
Sean Grebey
Sean Grebey
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 87
SQLKnowItAll (12/26/2012)
If it is as simple as what you provided... creating an index on Table2.FieldB will "probably" help. It will get rid of the scan and use a key lookup to get the data for the results. Again, "probably" if it is as simple as you have shown.


Thanks.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87073 Visits: 45267
SQLKnowItAll (12/26/2012)
If it is as simple as what you provided... creating an index on Table2.FieldB will "probably" help.


Depends how selective the filter is. It's definitely not a covering index, so depends on whether the cost of the key lookups is judged low enough (and that's assuming the filter's SARGable in the first place)

My gut feel is that it probably won't help. There's an index that appears covering, though the columns are in the wrong order to support the filter)

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


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