2013-02-13 119 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分钟容易地与单个连接)。