0

我已经给了一个任务来优化下面的 sql 查询。目前查询超时并导致大量阻塞。我刚开始使用 t-sql,所以请帮我优化查询。

select ExcludedID 
from OfferConditions with (NoLock) 
where OfferID = 27251 
  and ExcludedID in (210,223,409,423,447,480,633,...lots and lots of these...,
  13346,13362,13380,13396,13407,1,2) 

union 

select CustomerGroupID as ExcludedID 
from CPE_IncentiveCustomerGroups ICG with (NoLock) 
inner join CPE_RewardOptions RO with (NoLock) 
on RO.RewardOptionID = ICG.RewardOptionID 
where RO.IncentiveID = 27251 
  AND ICG.Deleted = 0 and RO.Deleted = 0 and 
  and ExcludedUsers = 1 
  and CustomerGroupID in (210,223,409,423,447,480,633,...lots and lots of these...,
  13346,13362,13380,13396,13407,1,2);
4

2 回答 2

1

您可以尝试将这些 ID 插入临时表并加入它,而不是使用 IN 语句。

于 2013-09-28T04:14:18.320 回答
0

解决问题的关键不是修复 SQL,而是修复表上的索引。例如,您应该在带有 OfferID 和 ExcludedID 的 OfferConditions 表上有一个复合索引。

在其他表上创建索引时,请记住,如果该字段位于 where OR 连接过滤器中,则它应该是复合索引的一部分。

于 2013-09-27T22:17:44.247 回答