2013-02-13 123 views
0

我有下面的sql語句,最多5分鐘內運行。當我添加任何連接時,sql語句會一直運行,直到超時。我想知道你是否可以讓我知道爲什麼添加到sql語句導致這種情況?這些都是加入,我已經添加到基礎SQL:其他加入減慢性能

添加了此聲明,基地SQL

inner join ahsrelate.dbo.ahs_encounter ahs_encounter_1 
on AHS_Encounter_1.PatientID=AHS_Patient.ID 

或添加此聲明,以基地SQL

inner join AHS_Medication 
on ahs_medication.patientid=AHS_Patient.ID 

基本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 
+3

你正在使用哪些DBMS?你能附上解釋計劃嗎?你在加入的列上有索引嗎? – 2013-02-13 23:46:11

+0

你在說什麼叫做性能調整。無論您使用的是什麼DBMS,都是一個龐大的主題,無法在解決stackoverflow問題的上下文中解決。您最好的辦法是發佈解釋或執行計劃,試圖讓我們確定您的查詢中存在哪些問題。 – 2013-02-13 23:48:18

+0

我正在使用Microsoft SQL。我在所有連接上都有索引。我無法制定解釋計劃。 – QYT 2013-02-13 23:48:39

回答

0

我猜不知道你的數據結構的細節,但我做基於我自己的醫療保健數據庫以前的工作有把握的猜測。我看看這個,再看看你的查詢:

inner join AHS_Medication 
on ahs_medication.patientid=AHS_Patient.ID 

,想到的第一件事是,你有一個病人,誰可能有多個問題,並多次遭遇,以及多種藥物,結果是你加入一些不相關的東西,從而產生更多的記錄而不是有意義的。對於查詢來說,5分鐘已經很長時間了,我敢打賭,藥物表格相當龐大,因此您將顯着增加運行時間。

考慮您的病人:

Patient1 
Patient2 
Patient3 

與交鋒加入(假設每個患者有兩次交鋒):

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

這是好的,但你用的藥物加入,要麼是不相同的分層結構,或者你忽略了將藥物與處方相關的加入標準(drug.PatientId & & medication.EncounterWhichPrescibed)。如果Patient1擁有三種藥物,那麼它會在每次遭遇中得到重複,因爲Encounter和Medication之間沒有關係(或者至少您沒有在連接標準中使用它)。

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 

這種問題可以出現其他連接以及因此每個無意義的聯接是增加運行幾何(即,5分鐘到可能成50分鐘容易地與單個連接)。