| | |
need help optimizing
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
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
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
•
•
Join Date: Jun 2005
Posts: 71
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
- Internet Speeds (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Advertising Your Site for Free (Promotion and Marketing Plans)
- Optimizing going rate... (Search Engine Optimization)
- Optimizing a homepage (Search Engine Optimization)
- SEOing your forum and optimizing your database (Social Media and Online Communities)
- tell me why? (Getting Started and Choosing a Distro)
- Scratch disk + Disk space (Storage)
- Efficient Programming (Computer Science)
Other Threads in the MS SQL Forum
- Previous Thread: To retrive Recordno with records
- Next Thread: Range of records in Top clause
Views: 1788 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday





