943,728 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1962
  • MS SQL RSS
Feb 15th, 2006
0

need help optimizing

Expand Post »
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
Similar Threads
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Feb 16th, 2006
0

Re: need help optimizing

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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Kate Albany is offline Offline
71 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: To retrive Recordno with records
Next Thread in MS SQL Forum Timeline: Range of records in Top clause





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC