Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Right way to Join

Author  Topic 

carumuga
Posting Yak Master

174 Posts

Posted - 2009-02-03 : 04:15:27
Hi,

I have two tables, Emp and EmpDetail, connected by using PK-FK.

What is the right way to join the table to get optimize result.
That is, Is it right the join

Emp.EmpID = EmpDetail.EmpID
or
EmpDetail.EmpID = Emp.EmpID

What i'm trying to post is, the better way to match the records by the concept of small table vs large table. One of my architect explained that try to related tables with large table with small table. (large table in the left hand side of the INNER JOIN).

Please advise.







bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-03 : 04:19:06
there will be no problem in inner join for to get the records (it will see that matched records will be displayed)

select e.col1,e.col2,e1.col1,.....(required columns)
from emp e
inner join empdetails e1 on e1.empid = e.empid
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2009-02-03 : 04:24:39
the order of tables for an inner join is not important

Em
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-03 : 04:30:17
Its work fine either join with Emp.EmpID = EmpDetail.EmpID
or
EmpDetail.EmpID = Emp.EmpID
Go to Top of Page

AvanthaSiriwardana
Yak Posting Veteran

78 Posts

Posted - 2009-02-03 : 04:43:33
But in order to optimize the query, you may first get the table which the data load is lower than the other table and then join it.

Avantha Siriwardana
Beware of bugs in the above code; I have only proved it correct, not tried it.
(Donald Knuth)
Go to Top of Page

carumuga
Posting Yak Master

174 Posts

Posted - 2009-02-03 : 04:51:46
Can i take it granted that there is no concept of large table joined with small table for optimizing?
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2009-02-03 : 05:00:03
either way round, the execution plan is the same

Em
Go to Top of Page

AvanthaSiriwardana
Yak Posting Veteran

78 Posts

Posted - 2009-02-03 : 05:16:48
All these concepts will base on the table which you gonna select data..but my concern is, there is a impact when you use the above concept...
:)

Avantha Siriwardana
Beware of bugs in the above code; I have only proved it correct, not tried it.
(Donald Knuth)
Go to Top of Page

rhysmeister
Starting Member

6 Posts

Posted - 2009-02-03 : 08:53:54
On odd occasions I have seen instances where table order has mattered. The execution plan "should" be the same but may not be for complex queries.

Twitter Integration | Ping.fm Integration | SQL Server Blog
Go to Top of Page
   

- Advertisement -