更新:我会尽快得到查询计划.
我们的查询效果不佳,需要4分钟才能完成特定组织.在通常的重新编译之后,存储的proc和更新统计信息没有帮助,我们将if Exists(...)重写为select count(*)...以及4分钟到70毫秒的存储过程.条件使得70毫秒查询需要4分钟的问题是什么?查看示例
这些都需要4分钟以上:
if ( SELECT COUNT(*) FROM ObservationOrganism omo JOIN Observation om ON om.ObservationID = omo.ObservationMicID JOIN Organism o ON o.OrganismID = omo.OrganismID JOIN ObservationMicDrug omd ON omd.ObservationOrganismID = omo.ObservationOrganismID JOIN SIRN srn ON srn.SIRNID = omd.SIRNID JOIN OrganismDrug od ON od.OrganismDrugID = omd.OrganismDrugID WHERE om.StatusCode IN ('F', 'C') AND o.OrganismGroupID <> -1 AND od.OrganismDrugGroupID <> -1 AND (om.LabType <> 'screen' OR om.LabType IS NULL)) > 0 print 'records';
-
IF (EXISTS( SELECT * FROM ObservationOrganism omo JOIN Observation om ON om.ObservationID = omo.ObservationMicID JOIN Organism o ON o.OrganismID = omo.OrganismID JOIN ObservationMicDrug omd ON omd.ObservationOrganismID = omo.ObservationOrganismID JOIN SIRN srn ON srn.SIRNID = omd.SIRNID JOIN OrganismDrug od ON od.OrganismDrugID = omd.OrganismDrugID WHERE om.StatusCode IN ('F', 'C') AND o.OrganismGroupID <> -1 AND od.OrganismDrugGroupID <> -1 AND (om.LabType <> 'screen' OR om.LabType IS NULL)) print 'records'
这一切都需要70毫秒:
Declare @recordCount INT; SELECT @recordCount = COUNT(*) FROM ObservationOrganism omo JOIN Observation om ON om.ObservationID = omo.ObservationMicID JOIN Organism o ON o.OrganismID = omo.OrganismID JOIN ObservationMicDrug omd ON omd.ObservationOrganismID = omo.ObservationOrganismID JOIN SIRN srn ON srn.SIRNID = omd.SIRNID JOIN OrganismDrug od ON od.OrganismDrugID = omd.OrganismDrugID WHERE om.StatusCode IN ('F', 'C') AND o.OrganismGroupID <> -1 AND od.OrganismDrugGroupID <> -1 AND (om.LabType <> 'screen' OR om.LabType IS NULL); IF(@recordCount > 0) print 'records';
对我来说,为什么将完全相同的Count(*)
查询移动到if语句会导致这种降级或者为什么'Exists'慢于Count
.我甚至尝试过exists()
a select CASE WHEN Exists()
,它仍然是4分钟以上.