Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

JOINing two tables with 2 SELECT Statements Expand / Collapse
Author
Message
Posted Saturday, October 20, 2012 8:39 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 13, 2014 5:21 PM
Points: 93, Visits: 521
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.
Post #1375139
Posted Sunday, October 21, 2012 10:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:12 AM
Points: 7,791, Visits: 9,545
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
Post #1375181
Posted Tuesday, October 23, 2012 12:25 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 13, 2014 5:21 PM
Points: 93, Visits: 521
Great advice. Thanks Tom.
Post #1375845
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse