Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9279023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:31:33+00:00 2026-06-18T17:31:33+00:00

I have the sql statement below that runs in 5 minutes max. When I

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:31:34+00:00Added an answer on June 18, 2026 at 5:31 pm

    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:

    inner join AHS_Medication
    on ahs_medication.patientid=AHS_Patient.ID
    

    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:

    Patient1
    Patient2
    Patient3
    

    Joined with encounters(assuming each patient has two encounters):

    Patient1  Encounter1
    Patient1  Encounter2
    Patient2  Encounter3
    Patient2  Encounter4
    Patient3  Encounter5
    Patient3  Encounter6
    

    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).

    Patient1  Encounter1  MedicationA
    Patient1  Encounter1  MedicationB
    Patient1  Encounter1  MedicationC
    Patient1  Encounter2  MedicationA
    Patient1  Encounter2  MedicationB
    Patient1  Encounter2  MedicationC
    Patient2  Encounter3  MedicationD
    Patient2  Encounter3  MedicationE
    Patient2  Encounter3  MedicationF
    Patient2  Encounter4  MedicationD
    Patient2  Encounter4  MedicationE
    Patient2  Encounter4  MedicationF
    Patient3  Encounter5  MedicationG
    Patient3  Encounter5  MedicationH
    Patient3  Encounter5  MedicationI
    Patient3  Encounter6  MedicationG
    Patient3  Encounter6  MedicationH
    Patient3  Encounter6  MedicationI
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below code that splits the sql statement and give its indices
I have a problem with the SQL statement detailed below. The query returns the
I have a sql statement that concatenates two columns, ItemNumber and Name, into one
I have a sql statement that uses Difference => http://msdn.microsoft.com/en-us/library/ms188753.aspx I do this in
I have an SQL statement that I'm executing through OleDb, the statement is something
This is the SQL statement that I have. SELECT USER_PROFILE.FIRST_NAME, USER_PROFILE.LAST_NAME, USER_PROFILE.USER_TYPE FROM USER_PROFILE
Problem that i face: -I have an input string, a SQL statement that i
I am using wordpress and I have an SQL statement (as below) to gather
I have a T-SQL statement that basically does an insert and OUTPUTs some of
I am working on a SQL Statement that I can't seem to figure out.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.