我正在试图查询一个医疗保健系统,这个系统可以统计没有任何评估的病人数量(病人在访问诊所时所经历的活动)。我想筛选出特定网络、用户和诊所的结果。
这是我的疑问:
select count(*) as Qualify from
UserClinic uc
INNER JOIN Patient p on p.DefaultClinicId = uc.ClinicId and uc.UsersId = 47 and p.NetworkId = 2
where
p.ActivePatient = 1
and p.deleted = 0
and not exists(select * from PatientToxicologyTesting ptt where ptt.PatientID = p.PatientID)
and not exists(select * from PatientPrescriptionRegistry ppr where ppr.PatientID = p.PatientID)
and not exists(select * from PatientPillCount pc where pc.PatientID = p.PatientID)
and not exists(select * from PatientControlledSubstanceAgreement csa where csa.PatientID = p.PatientID)
and not exists(select * from PatientHealthAssessment mha where mha.PatientID = p.PatientID)UserClinic是用户和诊所之间的多对多关系表.病人与网络和诊所相连。
上面的查询需要3分钟的访问时间才能执行,如果从查询中删除UserId或NetworkId连接条件,则这一次将大大减少到1秒。
有人能提出更好的方法来写这个查询吗?
编辑
以下是查询的执行计划


发布于 2018-10-05 07:11:07
很少有小窍门:
PatientID部件中使用的表上的NOT EXISTS列是否有索引。应该检查这些表:PatientToxicologyTesting, PatientPrescriptionRegistry, PatientPillCount, PatientControlledSubstanceAgreement and PatientHealthAssessment。您可以进行群集索引扫描,这意味着索引丢失或支离破碎。如果索引是高度分段的,则重新构建/重新组织它。Patient表上创建筛选索引以支持此查询。
Create Index IX_Patient_Filter_1 ON Patient(NetworkId,DefaultClinicId) WHERE ActivePatient = 1 AND deleted = 0UserClinic表上创建支持索引。
Create Index IX_UserClinic_1 ON UserClinic(UsersId,ClinicId )https://stackoverflow.com/questions/52659517
复制相似问题