Running sum performance between subquery VS. domain function (1 Viewer)

spenzer

Registered User.
Local time
Today, 18:45
Joined
Oct 19, 2011
Messages
42
I have been reading about the performance between subqueries and domain functions when applied in a query although both are slow,
I've always found that in the discussion it is the subquery that has an extra edge in terms of performance aspect.

However, recently I've created a running sum on a query intended to be used as the recordset of a form: I have tried these two(2) methods:

Method A - Domain Function
Code:
SELECT tblReceivables.*, DSum("Credit","tblReceivables","TransactionID = " & [TransactionID] & " AND ReceivableID <= " & [ReceivableID])-NZ(DSum("Debit","tblReceivables","TransactionID = " & [TransactionID] & " AND ReceivableID < " & [ReceivableID] & " AND [Active] = 0"),0) AS Balance
FROM tblReceivables
ORDER BY tblReceivables.ReceivableID DESC;

Method B - Subquery
Code:
SELECT tblReceivables.*, (SELECT SUM(Credit) FROM tblReceivables as REC WHERE REC.TransactionID = tblReceivables.TransactionID AND REC.ReceivableID <= tblReceivables.ReceivableID) - NZ((SELECT SUM(Debit) FROM tblReceivables as REC WHERE REC.TransactionID = tblReceivables.TransactionID AND REC.ReceivableID < tblReceivables.ReceivableID AND Active = False),0) AS Balance
FROM tblReceivables
ORDER BY tblReceivables.ReceivableID DESC;


Performance Result:

Method A - .05 seconds
Method B - 20 seconds

Both does the job as intended.

But method B (subquery) is excruciatingly slow and sometimes "very seldom" it crashes ms access 2007.

Now, basing from the various articles and discussions i have found in the web; it is the subquery which is supposed to be faster than the two.

What am i doing wrong with my subquery snippet, why has it become as slow as molasses? Is there anything i can do to make this query any faster?
 

Guus2005

AWF VIP
Local time
Today, 12:45
Joined
Jun 26, 2007
Messages
2,641
Domain functions like DSum are faster on local tables. When the table is attached a subquery or a use of a different (TLOOKUP) function should be faster.

Also when the backend is SQL Server using pass-through queries will speed up any query.

HTH:D
 

Users who are viewing this thread

Top Bottom