以下查询花费了太多时间(超过 10 分钟)。有没有办法让它更快?
select r.id,rs1.Id, rs2.Id
from Resource rs1 , Resource rs2 , ResourceTerritory rst1 ,
ResourceTerritory rst2 ,
ReleaseResource rr1 ,
ReleaseResource rr2 , Release r
where
rs1.Id=rst1.ResourceId and rs2.Id=rst2.ResourceId and rs1.Id=rr1.ResourceId and rs2.Id=rr2.ResourceId
and rr1.ReleaseId=rr2.ReleaseId
and rs1.Id<>rs2.id
and rs1.OwningTerritoryId=69 and rs2.OwningTerritoryId=200
and r.Id=rr1.ReleaseId and r.OwningTerritoryId=69 and rs1.IsLocked=0 and rs2.IsLocked=0
group by r.id,rs1.Id, rs2.Id
having SUM( case when rst1.TerritoryId = 62 then 1 else 0 end)>0 and
SUM( case when rst1.TerritoryId = 69 then 1 else 0 end)>0 and
SUM( case when rst1.TerritoryId = 200 then 1 else 0 end)>0 and
SUM( case when rst1.TerritoryId = 201 then 1 else 0 end)>0 and
SUM( case when rst2.TerritoryId = 69 then 1 else 0 end)>0 and
SUM( case when rst2.TerritoryId = 62 then 1 else 0 end)>0 and
SUM( case when rst2.TerritoryId = 200 then 1 else 0 end)>0 and
SUM( case when rst2.TerritoryId = 201 then 1 else 0 end)=0