我已經給出了一個任務來優化下面的sql查詢。目前查詢超時並導致大量阻塞。我剛開始使用t-sql,所以請幫助我優化查詢。Sql Server聯合查詢優化
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);
這個硬編碼ID的大列表來自哪裏?你確定你需要UNION而不是UNION ALL嗎?你看過實際執行計劃,看看瓶頸在哪裏嗎? –
ID是來自代碼,代碼實際上構建一個字符串,我想它是通過它,我沒有細節爲什麼他們選擇UNION而不是聯盟所有 – user2041076
此外,我不知道我明白這個查詢可能是如何造成很多阻塞... –