I have the sql statement below that runs in 5 minutes max. When I add any joins to it, the sql statement runs until it times out. I was wondering if you could let me know why adding to the sql statement causes this? These are the joins that I have added to the base SQL:
Added this statement to Base SQL
inner join ahsrelate.dbo.ahs_encounter ahs_encounter_1
on AHS_Encounter_1.PatientID=AHS_Patient.ID
Or Added this Statement to Base SQL
inner join AHS_Medication
on ahs_medication.patientid=AHS_Patient.ID
Base SQL
SELECT distinct
AHS_Patient.FullName,
AHS_Patient_Iorg.OrganizationMrn,
AHS_Patient.SexName,
AHS_Patient.DateOfBirth,
Finding1.NumericResult,
Finding2.NumericResult,
AHS_Problem.ICD9DiagnosisCode,
AHS_Encounter.EncounterDTTM,
AHS_Encounter.EncounterTypeName,
AHS_Result.EntryCode,
AHS_Result_1.EntryCode,
AHS_Result.NumericResult,
AHS_Result_1.NumericResult,
AHS_Result.ClinicalDTTM,
AHS_Result_1.ClinicalDTTM,
AHS_Provider.FullName,
AHS_Problem.Problem,
AHS_Problem.ProblemStatusName,
AHS_Encounter.ApptLocationName,
AHS_Encounter.AppointmentStatusName
FROM AHSRelate.dbo.AHS_Patient AHS_Patient
INNER JOIN AHSRelate.dbo.Finding1 Finding1
ON AHS_Patient.ID=Finding1.PatientID
AND Finding1.EntryMnemonic='BP SYS'
INNER JOIN AHSRelate.dbo.Finding2 Finding2
ON AHS_Patient.ID=Finding2.PatientID
AND Finding2.EntryMnemonic='BP DIAS'
INNER JOIN AHSRelate.dbo.AHS_Result AHS_Result
ON AHS_Patient.ID=AHS_Result.PatientID
AND AHS_Result.EntryCode IN ('D5353078', 'Q25015900')
INNER JOIN AHSRelate.dbo.AHS_Result AHS_Result_1
ON AHS_Patient.ID=AHS_Result_1.PatientID
AND AHS_Result_1.EntryCode IN ('D5353037', 'Q25003000')
INNER JOIN AHSRelate.dbo.AHS_Encounter AHS_Encounter
ON AHS_Encounter.PatientID=AHS_Patient.ID
AND AHS_Encounter.AppointmentStatusName='Pending'
AND AHS_Encounter.EncounterTypeName='Appointment'
and AHS_Encounter.EncounterDTTM >= getdate()-1
and AHS_Encounter.EncounterDTTM <= getdate()+1
INNER JOIN AHSRelate.dbo.AHS_Problem AHS_Problem
ON AHS_Patient.ID=AHS_Problem.PatientID
INNER JOIN AHSRelate.dbo.AHS_Patient_Iorg AHS_Patient_Iorg
ON AHS_Patient.ID=AHS_Patient_Iorg.PersonID
inner JOIN AHSRelate.dbo.AHS_Provider AHS_Provider
ON AHS_Encounter.Provider2ID=AHS_Provider.ID
ORDER BY
AHS_Patient.FullName,
AHS_Result.ClinicalDTTM DESC,
AHS_Result_1.ClinicalDTTM DESC
I am guessing without knowing the details of your data structure, but am making an educated guess based on my own previous work with health care databases. I look at this and then look at your query:
The first thing that comes to mind is that you have a Patient, who might have multiple problems, and multiple encounters, and multiple medications, and the result is that you are joining things together that aren’t related and thus producing far more records than makes sense. 5 minutes is already a long time for a query, and I would bet the medications table is pretty huge comparatively, and thus you are going to multiply the runtime significantly.
Consider your patients:
Joined with encounters(assuming each patient has two encounters):
This is fine, but then you join with medications, which is either not in the same hierarchy or you left out join criteria that relates the medication to the Encounter in which it was prescribed(medication.PatientId && medication.EncounterWhichPrescibed). If Patient1 has three medications, it will get duplicated for each encounter, because there is no relationship between Encounter and Medication(or at least you didn’t use it in the join criteria).
This kind of problem could be occurring for other joins as well and thus each non-sensical join is increasing the runtime geometrically(i.e. 5 minutes could to into 50 minutes easily with a single join).