DATEADD 函数的性能问题

Performance issue with DATEADD function

这是我的实际 select 查询,

SELECT b.CaseNumber as CaseNumber,b.DebtorNr ,b.ActionDate,DATEADD(MONTH,-12,b.ActionDate) one_month,a.Registerdate --COALESCE(count(A.historynr),0) as DebtorActivity  
                from rr..r_basic_info b 
                join rr..activities_VW as A on b.DebtorNr=a.Debtornr
                where 
                    B.Debtornr = A.Debtornr 
                    --and a.Registerdate<=b.ActionDate --this condition works
                    and a.registerdate >= DATEADD(month,-12,getdate()) --i have a problem with this condition and causing huge time consumption

我这里定义的视图是activities_VW

select H.NR as historynr,o.debtornr as Debtornr, O.NR as ordernr, h.Actmenunr as Actmenunr,h.AGREEMENT as AgreementCode, h.Registerdate as Registerdate

来自 abc..history h join abc..orders o on o.NR=h.ORDERNR

我的执行计划就像

所有行的更多信息 b.actionDate 列具有相同的值,例如“2015-04-11 08:37:44.037”。
我检查了所有日期格式,但没有发现任何错误。
对于另一种情况,我对 b.actionDate 列中的不同行有不同的值,并且它在这种情况下工作正常。

谢谢

我的理解可能有误,所以只把它当作一种可能性 - 当在连接条件 and/or where 子句中使用函数时,为了确定数据是否符合条件,必须检查它针对 table.

中的每一行

想想你的第一部分 WHERE e.DATE <= a.joining_date - 你可以直接查看小于 e.DATE 的行。

对于您的第二部分 AND e.DATE >= DATEADD(MONTH, - 6, a.joining_date) - 没有列是 "joining date minus 6 months",因此要确定 e.Date 是否大于它,您需要对每个列执行该计算table 中 a.joining_date 的实例。

请记住,where 子句信息 不一定按顺序计算 是否在查询中记录下来 - 因此您认为的行已被 [= =15=]不一定被它淘汰。因此,正如其中一条评论所建议的那样,在 DATEADD(MONTH, - 6, a.joining_date) 上使用 computed/persisted 列可能效果很好。

这是我的实际 select 查询,

SELECT b.CaseNumber as CaseNumber,b.DebtorNr ,b.ActionDate,DATEADD(MONTH,-12,b.ActionDate) one_month,a.Registerdate --COALESCE(count(A.historynr),0) as DebtorActivity  
                from rr..r_basic_info b 
                join rr..activities_VW as A on b.DebtorNr=a.Debtornr
                where 
                    B.Debtornr = A.Debtornr 
                    --and a.Registerdate<=b.ActionDate --this condition works
                    and a.registerdate >= DATEADD(month,-12,getdate()) --i have a problem with this condition and causing huge time consumption

我这里定义的视图是activities_VW

select H.NR as historynr,o.debtornr as Debtornr, O.NR as ordernr, h.Actmenunr as Actmenunr,h.AGREEMENT as AgreementCode, h.Registerdate as Registerdate

来自 abc..history h join abc..orders o on o.NR=h.ORDERNR

我的执行计划就像

所有行的更多信息 b.actionDate 列具有相同的值,例如“2015-04-11 08:37:44.037”。
我检查了所有日期格式,但没有发现任何错误。
对于另一种情况,我对 b.actionDate 列中的不同行有不同的值,并且它在这种情况下工作正常。

谢谢