need help optimizing

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

need help optimizing

 
0
  #1
Feb 15th, 2006
trying to get a count of transfers from one account to another account within the same bank
relevant columns:

table transfers
transferid, toaccount, fromaccount

table accounts
accountid, bank

what i have now is
select count(*) from transfers where
toaccount in (select accountid from accounts where bank = @bank)and
fromaccount in(select accountid from account where where bank = @bank)

this works but is extremely slow as the accounts table is large and this has to run through it twice.

any ideas for optimizing this query
also, I don't know if this will help, but all I really care about is if there are any or not, the exact count doesn't really matter
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 71
Reputation: Kate Albany is an unknown quantity at this point 
Solved Threads: 1
Kate Albany Kate Albany is offline Offline
Junior Poster in Training

Re: need help optimizing

 
0
  #2
Feb 16th, 2006
campkev,

I don't have much experience with Microsoft SQL Server, I use Oracle. However this is how I would approch it in Oracle. The syntax may be different in SQL Server but the principal will be the same.

select count(1)
from transfer t, account a1, account a2,
where t.toaccount = a1.accountid
and t.fromaccount = a2.accountid
and a1.bank = a2.bank

If there are appropiate indexes on the tables in question this should be fast.

By the way, using multiple 'in' statements like you posted is very expensive. Using joins is prefered.

Kate
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC