Slow MS Access Query

How can I optimize this query. As written, it takes 10's of minutes to execute.

SELECT C.CustomerId AS [Customer Id], IIf(Len([Surname])>1,IIf(Len([Title])>1,[Title] & " ","") & Trim([FirstName]) & " " & Trim([Surname]) & IIf(Len([Suffix])>1,", " & [Suffix],""),[Business]) AS Customer, R.MailingsId, R.DateCompleted AS [Date], R.Value, R.Cost, dbo_tblSalesSheetDetail.Commission, dbo_tblSalespeople.SalesLastName AS Rep, (Select Count(Seq) From dbo_tblReturns Where CustomerId = R.CustomerId and MailingsId > 0) AS Responses, (Select Max(DateCompleted) From dbo_tblReturns Where dbo_tblReturns.CustomerId = C.CustomerId and dbo_tblReturns.DateCompleted < R.DateCompleted) AS [Last]
FROM ((dbo_tblReturns AS R INNER JOIN dbo_tblCustomer AS C ON R.CustomerId = C.CustomerId) INNER JOIN dbo_tblSalespeople ON R.SalesId = dbo_tblSalespeople.SalesId) LEFT JOIN dbo_tblSalesSheetDetail ON R.Seq = dbo_tblSalesSheetDetail.Seq
WHERE (((R.MailingsId)=[Forms]![frmMailingDetail]![MailingsId]))
ORDER BY R.MailingsId, R.DateCompleted;

It is the following that slows this query:

(Select Max(DateCompleted) From dbo_tblReturns Where dbo_tblReturns.CustomerId = C.CustomerId and dbo_tblReturns.DateCompleted < R.DateCompleted) AS [Last]

Without it, it executes in a fraction of a second.
Douglas CummingsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
Dmax is an aggregate function, so it will take a while to run through a large recordset to get the Max value for each customer.

You can try filtering the records down to a more manageable size.
For example, instead of searching ALL Years, limit the search to just this years records.
Or, ...Instead of searching all customers, ...limit it to just active customers.

Lets see what others ma suggest...

Access doesn't optimize sub queries very well.  You might get some improvement by creating separate queries for the sub selects and joining to them.  Also, try adding an index on DateCompleted.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Douglas CummingsAuthor Commented:
Thanks for the help.

At Pat's suggestion, I created a separate query for the sub select and using Jeff's suggestion, I limited the number of customers to only those that responded to the mailing that was the basis for the underlying query. DateCompleted is indexed. The problem, as Jeff suggested, was 649,000 customer records and 1.6 million order records.

Jeffrey CoachmanMIS LiasonCommented:
Other tips:
1. Limit the fields to only the fields absolutely needed.
2. You might also get a little bump in speed by checking for:
Not IsNull(YourField)
...instead of LEN(YourField)>1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.