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


JOINing two tables with 2 SELECT Statements


JOINing two tables with 2 SELECT Statements

Author
Message
TrailRunner
TrailRunner
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 654
I am joining 2 tables of 30 columns with 5 mil and 50 columns with 10 mil rows. Now, I only need 5 columns from the 5 mil table and 15 columns from the 10 mil table. What is the best way to join these two tables to gain the best performance? I simplified a real issue I am facing with the above question. I can do the following:

do a inner join of the two tables by name and return the 20 columns from the SELECT list. I need to return all rows.
do a inner join of two SELECT statements made for each tables returning only the needed columns.

I am getting mixed results, and therefore want to take a poll from the Pros here...Thanks a lot for helping.
Tom Thomson
Tom Thomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14632 Visits: 12238
Don't try to write anything over-complicated. The KISS principal applies here.
Keep the join condition separate from any other filters, which should be written as where conditions.
Try to use only SARG-able predicates in where and join conditions which you are trying to support with indexes.
Make sure you have indexes which will support the query, but only provided they won't cause as much or more pain on other operations than they save on this one.
Don't have any query hints/table hints or anything like that unless you can prove that it's useful.

Tom

TrailRunner
TrailRunner
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 654
Great advice. Thanks Tom.
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