首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化需要很长时间才能带来数据的SQL查询

优化需要很长时间才能带来数据的SQL查询
EN

Stack Overflow用户
提问于 2018-10-05 06:27:52
回答 1查看 55关注 0票数 0

我正在试图查询一个医疗保健系统,这个系统可以统计没有任何评估的病人数量(病人在访问诊所时所经历的活动)。我想筛选出特定网络、用户和诊所的结果。

这是我的疑问:

代码语言:javascript
复制
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秒。

有人能提出更好的方法来写这个查询吗?

编辑

以下是查询的执行计划

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-05 07:11:07

很少有小窍门:

  1. 不要使用'COUNT(*)‘或'SELECT *’,如果您对尝试这样选择的特定值不感兴趣的话。我建议使用“计数(1)”或“选择1”
  2. 重复检查在PatientID部件中使用的表上的NOT EXISTS列是否有索引。应该检查这些表:PatientToxicologyTesting, PatientPrescriptionRegistry, PatientPillCount, PatientControlledSubstanceAgreement and PatientHealthAssessment。您可以进行群集索引扫描,这意味着索引丢失或支离破碎。如果索引是高度分段的,则重新构建/重新组织它。
  3. 可以在Patient表上创建筛选索引以支持此查询。 Create Index IX_Patient_Filter_1 ON Patient(NetworkId,DefaultClinicId) WHERE ActivePatient = 1 AND deleted = 0
  4. 可以在UserClinic表上创建支持索引。 Create Index IX_UserClinic_1 ON UserClinic(UsersId,ClinicId )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52659517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档