-4

次のクエリに時間がかかりすぎています (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 
4

3 に答える 3

0

(例) TerritoryIdにインデックスはありますか?

また、チェックしてください:http://msdn.microsoft.com/en-us/library/ms345434.aspx

SQL Server Profiler を実行します ([ツール] メニューの下)。

そして句の代わりに句WHEREを使用できますINNER JOIN

于 2013-06-11T08:30:31.073 に答える
0
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 

に置き換える必要があります

WHERE 
(rst1.TerritoryId = 62 
OR rst1.TerritoryId = 69 
OR rst1.TerritoryId = 200 
OR rst1.TerritoryId = 201) 
AND 
(rst2.TerritoryId = 62 
OR rst2.TerritoryId = 69 
OR rst2.TerritoryId = 200 
)
于 2013-06-11T08:33:46.287 に答える