0

下面的查询需要更长的时间来执行,因为表有超过 50M 的记录,而且查询中的 OR 条件似乎不正确......谁能为我的查询提供优化版本。欣赏..!!

    DECLARE @LastRunDate DATETIME
SELECT @LastRunDate=LastRunDateTime FROM [DataAudit].[t_DeltaSetting] da 
WHERE da.[InterfaceName] = 'ATKInnovationTargetedCustomersToHANA'


SELECT distinct (initi.RecordID )
from WebData.t_Initiative initi
              inner join WebData.t_InitiativeCustomer ic on initi.RecordID=ic.InitiativeId
              inner join WebData.t_Tracker track on track.InitiativeId = initi.RecordID
              inner join WebData.t_TrackerCustomer tc on tc.TrackerId=track.RecordID

              where (initi.UpdatedDate >  @LastRunDate) 
              or (ic.UpdatedDate > @LastRunDate) 
              or (track.UpdatedDate > @LastRunDate) 
              or (tc.UpdatedDate > @LastRunDate)
4

1 回答 1

0

尝试使用union

SELECT initi.RecordID
FROM WebData.t_Initiative initi
WHERE initi.UpdatedDate > @LastRunDate
UNION
SELECT ic.InitiativeId
FROM WebData.t_InitiativeCustomer ic
WHERE ic.UpdatedDate > @LastRunDate
UNION
SELECT track.InitiativeId
FROM WebData.t_Tracker track
WHERE track.UpdatedDate > @LastRunDate
UNION
SELECT tc.TrackerId
FROM WebData.t_TrackerCustomer tc 
WHERE tc.UpdatedDate > @LastRunDate;

这假设InitiativeID始终是有效的RecordID. 如果不是,则需要额外的逻辑来进行验证(使用JOININEXISTS)。

于 2021-08-18T15:10:43.873 回答