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 2000 Forums
 Transact-SQL (2000)
 any solution to this?

Author  Topic 

tishri
Yak Posting Veteran

95 Posts

Posted - 2007-07-21 : 09:26:25
hey guys i have a problem with a query that took so long to execute. I have 500,000 records of payments.

Can you please suggest a faster query solution for the query below. Thanks..


I created index for tblpayments for faster query.
the indexes are ACCOUNT_NO, DATE_PAID

select * , (select sum(amount_paid) from tblpayments
where account_no = p.account_no and date_paid <= .date_paid)
as running_total
from tblpayments











TCC

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-21 : 09:43:31
Yes, in your front-end application.
Or try this. Took less than a second to run the staging part on my laptop.
-- Prepare sample data
USE TempDB

SET NOCOUNT ON

CREATE TABLE dbo.Payments
(
Account VARCHAR(5),
AmountPaid MONEY,
DatePaid DATETIME,
RunningTotal MONEY
)

DECLARE @Loop INT
SET @Loop = 1

WHILE @Loop <= 500000
BEGIN
INSERT dbo.Payments
SELECT ABS(CHECKSUM(NEWID())) % 10000,
ABS(CHECKSUM(NEWID())) % 10000,
ABS(CHECKSUM(NEWID())) % 10000,
0

SET @Loop = @Loop + 1
END

CREATE CLUSTERED INDEX IX_Payments ON dbo.Payments (Account, DatePaid)

-- Stage the data
DECLARE @Total MONEY,
@Account VARCHAR(5)

SELECT @Total = 0,
@Account = ''

UPDATE dbo.Payments
SET @Total = RunningTotal = CASE WHEN @Account = Account THEN @Total + AmountPaid ELSE AmountPaid END,
@Account = Account

-- Show the expected output
SELECT Account,
AmountPaid,
DatePaid,
RunningTotal
FROM Payments
ORDER BY Account,
DatePaid

-- Clean up
DROP TABLE dbo.Payments

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-21 : 09:43:49
do it in the front end ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

tishri
Yak Posting Veteran

95 Posts

Posted - 2007-07-21 : 10:07:07
but my front end computers are slow computers... but if its really only solution i think i should stick to my old query. I still have to use the server side at least i only have one computer to increase the speed and memory. but anyway thanks.,..

TCC
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-21 : 10:11:44
Sending 500000 records to the application is still the slowest part in this operation...
The "slow" computers will be able to calculate the running total faster than receiving the records anyway!


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -