Link to home
Start Free TrialLog in
Avatar of Scotto123
Scotto123

asked on

Access only runs on one processor?

I was running this query today and my PC was freaking out!

SELECT Patient.PatientID, Patient.VisitID, Patient.AdmitDateTime, (select max([AdmitDateTime]) from Patient P where P.PatientID=Patient.PatientID and P.AdmitDateTime<Patient.AdmitDateTime) AS PrevAdmissionDate, DateDiff("d",[PrevAdmissionDate],[AdmitDateTime]) AS AdmissonDiff, Patient.DischargeDateTime,(select max([DischargeDateTime]) from Patient P where P.PatientID=Patient.PatientID and P.DischargeDateTime<Patient.DischargeDateTime) AS PrevDischargeDate, DateDiff("d",[PrevDischargeDate],[DischargeDateTime]) AS DischargeDiff
FROM Patient
ORDER BY Patient.PatientID, Patient.AdmitDateTime;

The query ran for 30 minutes.  

The CPU was maxed out and the RAM was all used up.  I learned that Access only runs on one of the 4 CPUs I have.  Is this right?  Seems crazy that my PC can handle so much and it doesn't utilize the full system.  Please let me know what I can do to speed up queries like this.
Avatar of Brian Harrington
Brian Harrington
Flag of United States of America image

What version are you using?
ASKER CERTIFIED SOLUTION
Avatar of Brian Harrington
Brian Harrington
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scotto123
Scotto123

ASKER

I have Access 2010.  I could run the data in SQL Server Mgt Studio.  I am anayzing data and want to be able to do it easier without having to build SQL tables and such.  Maybe I should change my analysis style.
Avatar of Dale Fye
It's your nested select statements that are causing the problem.

Try creating a query like this:

SELECT N.PatientID, N.AdmitDateTime, Max(P.AdmitDateTime) as PrevAdmit, Max(P.DischargeDateTime) as PrevDischarge
FROM Patients N
LEFT JOIN Patients P
ON N.PatientID = P.PatientID
AND P.DischargeDateTime < N.AdmitDateTime
GROUP BY N.PatientID, N.AdmitDateTime

That should give you every PatientID, Admission, and the patients most recent previous admission and discharge.  If that works, then use that as a subquery and join it to your Patients table on PatientID and AdmitDateTime to get the other fields you need from the Patients table.

But do you really need every patient/admit combination?  If not you could limit the results of this query significantly by adding a WHERE clause on the N.AdmitDateTime field.  Or by minimizing the number of records in the N part of the query by using something like:

SELECT N.PatientID, N.AdmitDateTime, Max(P.AdmitDateTime) as PrevAdmit, Max(P.DischargeDateTime) as PrevDischarge
FROM (SELECT PatientID, MAX(AdmitDateTime) as MostRecentVisit FROM Patients) as N
LEFT JOIN Patients P
ON N.PatientID = P.PatientID
AND P.DischargeDateTime < N.MostRecentVisit
GROUP BY N.PatientID, N.AdmitDateTime

By using the subquery in this, you limit N to one record per patient, and the resulting recordset, with the PrevAdmit and PrevDischarge will only contain one record as well.
You anwered my question harrington.  Thanks!