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)
 Which is a better query?

Author  Topic 

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-02-09 : 23:10:57
Both of the following queries work and return what I want, but which method is better to use. The one with a Join or the one with a sub-query in the select statment?


Select bi.StartDate,bi.BenefitInterestID,0,0,bi2.Interst,0
From TSV_BenefitInterest bi left Join @TmpTblInterest ti
on ti.DateReceived between bI.StartDate and bI.EndDate
left join TSV_BenefitInterest bi2
on bi.BenefitTypeID = Bi2.BenefitTypeID
where ti.BenefitTypeID is null
and bi2.BenefitInterestID = (Select top 1 c.BenefitInterestID from TSV_BenefitInterest c where c.StartDate > bi.Startdate and c.BenefitTypeID = @BenefitTypeID order by c.Startdate asc)

--or

Select bi.StartDate,bi.BenefitInterestID,0,0,
(SELECT b.Interest FROM TSV_BenefitInterest AS b WHERE b.BenefitInterestID = (Select top 1 c.BenefitInterestID from TSV_BenefitInterest c where c.StartDate > bi.Startdate and c.BenefitTypeID = @BenefitTypeID order by c.Startdate asc)),0
From TSV_BenefitInterest bi left Join @TmpTblInterest ti
on ti.DateReceived between bI.StartDate and bI.EndDate
where ti.BenefitTypeID is null


Thanks, Both should work for me, I'm just worried about performance. My set I am working with isn't large enough for me to gage using analysers.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-10 : 08:51:36
How do the query/execution plan look like?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-02-10 : 18:48:33
my instinct is the join will have the better plan. of course neither will be very good if you don't have proper indexes.


www.elsasoft.org
Go to Top of Page
   

- Advertisement -