Connecting Tech Pros Worldwide Help | Site Map

Cumulative Sum in query is very slow...

cefrancke@yahoo.com
Guest
 
Posts: n/a
#1: Nov 13 '05
Are there any ways to speed up a Cumulative sum in a query or perhaps
another faster way to have a cumulative sum column (call a vba
function?).

For example, I want to sum up all values under 'Amount', and also have
a field call 'Cumulative' and have the sum grow with each record. Using
"MyTable", the query results are...

ID or Date Amount Cumulative
--------------- ------ ----------
a 2-3-2099 5 5
b 29-5-2099 3 8
c 12-8-2099 6 14
d 23-9-2099 2 16

My cumulative field uses,

Cumulative: DSum("[Amount]","MyTable","Date<=" & Date) 'For Dates

Cumulative: DSum("[Amount]","MyTable","ItemID<=" & ID) 'For ID's

This works well for relatively small recordsets, but for aggregate
'Group By' queries or large tables it is painfully slow.

Any ideas or comments?

TIA

Helen Wheels
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Cumulative Sum in query is very slow...


cefrancke@yahoo.com wrote:[color=blue]
> Are there any ways to speed up a Cumulative sum in a query or perhaps
> another faster way to have a cumulative sum column (call a vba
> function?).
>
> For example, I want to sum up all values under 'Amount', and also have
> a field call 'Cumulative' and have the sum grow with each record. Using
> "MyTable", the query results are...
>
> ID or Date Amount Cumulative
> --------------- ------ ----------
> a 2-3-2099 5 5
> b 29-5-2099 3 8
> c 12-8-2099 6 14
> d 23-9-2099 2 16
>
> My cumulative field uses,
>
> Cumulative: DSum("[Amount]","MyTable","Date<=" & Date) 'For Dates
>
> Cumulative: DSum("[Amount]","MyTable","ItemID<=" & ID) 'For ID's
>
> This works well for relatively small recordsets, but for aggregate
> 'Group By' queries or large tables it is painfully slow.
>
> Any ideas or comments?
>
> TIA
>[/color]

The domain aggregate functions can be painfully slow.

You could try using a subquery to define your "cumulative" field instead
of using a function. Your query SQL might look something like this:

SELECT
mytable.mydate,
Sum(mytable.amount) AS SumOfamount,
(select sum(amount) from mytable as mytablecopy
where mydate<=mytable.mydate) AS cumulative
FROM mytable
GROUP BY mytable.mydate;

I think this should run reasonably quickly as long as your date field is
indexed.

Or, there are much faster replacements for Access' slow built in domain
aggregate functions which are available from
http://www.mvps.org/access/modules/mdl0012.htm

Br@dley
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Cumulative Sum in query is very slow...


cefrancke@yahoo.com wrote:[color=blue]
> Are there any ways to speed up a Cumulative sum in a query or perhaps
> another faster way to have a cumulative sum column (call a vba
> function?).
>
> For example, I want to sum up all values under 'Amount', and also have
> a field call 'Cumulative' and have the sum grow with each record.
> Using "MyTable", the query results are...
>
> ID or Date Amount Cumulative
> --------------- ------ ----------
> a 2-3-2099 5 5
> b 29-5-2099 3 8
> c 12-8-2099 6 14
> d 23-9-2099 2 16
>
> My cumulative field uses,
>
> Cumulative: DSum("[Amount]","MyTable","Date<=" & Date) 'For Dates
>
> Cumulative: DSum("[Amount]","MyTable","ItemID<=" & ID) 'For ID's
>
> This works well for relatively small recordsets, but for aggregate
> 'Group By' queries or large tables it is painfully slow.
>
> Any ideas or comments?[/color]

Dsum() is definately very slow especially for large data sets.

I'm sure there is a nice dynamic way to do it... but...

You can always dump the data into a temporary table and run some code
through it to do the summing. It can be remarkable quick this way.

Br@dley


Steve Jorgensen
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Cumulative Sum in query is very slow...


As others have pointed out, domain funnctions are slow, but you have a more
fundamental problem. The time it takes to perform your sum is proportional to
n squared where n is the number of records. If you do it for 1000 records,
you've had to sum (I believe) 500500 records. That's why SQL doesn't do
running sums very well.

Workarounds:
1. Only do running sums on reports. Access will do a running sum for you
that's generated be keeping a running sum in memory as the rows are read from
the query.
2. Use a temporary result. Here, you first create a result table with an
autoincrement field, use an insert query to populate the table in order
(leaving the running sum blank), then use code to open a recordset on the
result table, loop through the rows using the autoincrement field as the sort
order, keep a running total, and write to the running total to the running sum
field.

On 20 Apr 2005 00:34:19 -0700, "cefrancke@yahoo.com" <cefrancke@yahoo.com>
wrote:
[color=blue]
>Are there any ways to speed up a Cumulative sum in a query or perhaps
>another faster way to have a cumulative sum column (call a vba
>function?).
>
>For example, I want to sum up all values under 'Amount', and also have
>a field call 'Cumulative' and have the sum grow with each record. Using
>"MyTable", the query results are...
>
> ID or Date Amount Cumulative
>--------------- ------ ----------
> a 2-3-2099 5 5
> b 29-5-2099 3 8
> c 12-8-2099 6 14
> d 23-9-2099 2 16
>
>My cumulative field uses,
>
>Cumulative: DSum("[Amount]","MyTable","Date<=" & Date) 'For Dates
>
>Cumulative: DSum("[Amount]","MyTable","ItemID<=" & ID) 'For ID's
>
>This works well for relatively small recordsets, but for aggregate
>'Group By' queries or large tables it is painfully slow.
>
>Any ideas or comments?
>
>TIA[/color]

Alan Webb
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Cumulative Sum in query is very slow...


CEFrancke,
I dumped the idea of doing cumulative or running sums in queries and went
with building a table where I store my cumulative totals. Since it's stored
as a calculated result all a query has to do is retrieve the row--very fast.
I also index my totals table because I am more interested in faster results
in querying than I am in faster posting of results. Sounding like data
warehouse technique? Yup.
--
Alan Webb
knoNOgeek@SPAMhotmail.com
"It's not IT, it's IS"

<cefrancke@yahoo.com> wrote in message
news:1113982459.795993.168290@z14g2000cwz.googlegr oups.com...[color=blue]
> Are there any ways to speed up a Cumulative sum in a query or perhaps
> another faster way to have a cumulative sum column (call a vba
> function?).
>
> For example, I want to sum up all values under 'Amount', and also have
> a field call 'Cumulative' and have the sum grow with each record. Using
> "MyTable", the query results are...
>
> ID or Date Amount Cumulative
> --------------- ------ ----------
> a 2-3-2099 5 5
> b 29-5-2099 3 8
> c 12-8-2099 6 14
> d 23-9-2099 2 16
>
> My cumulative field uses,
>
> Cumulative: DSum("[Amount]","MyTable","Date<=" & Date) 'For Dates
>
> Cumulative: DSum("[Amount]","MyTable","ItemID<=" & ID) 'For ID's
>
> This works well for relatively small recordsets, but for aggregate
> 'Group By' queries or large tables it is painfully slow.
>
> Any ideas or comments?
>
> TIA
>[/color]


Closed Thread


Similar Microsoft Access / VBA bytes