这种递归CTE永远运行(永远不会返回结果),当手动获取相同结果大约需要10秒时,其中大部分是复制粘贴。
RekeyLevel <= 2
这样的故障保护?当前查询:
with RekeysAllLevelsDeep as (
select
a.claimid as Rekey
,a.ClaimIDAdjFromOrig as Original
,0 as RekeyLevel
from <base table> (nolock) a
where a.ClaimIDAdjFromOrig is not null
and a.ClaimIDAdjFromOrig <> a.ClaimID
union all
select
a.claimid as Rekey
,a.ClaimIDAdjFromOrig as Original
,RekeyLevel + 1
from RekeysAllLevelsDeep
join <base table> (nolock) a
on RekeysAllLevelsDeep.Original = a.ClaimID
where a.ClaimIDAdjFromOrig is not null
and a.ClaimIDAdjFromOrig <> a.ClaimID
and RekeyLevel <= 2
)
select distinct
Rekey
,Original
,RekeyLevel
from RekeysAllLevelsDeep
where Original is not null
and Original <> Rekey
and Rekey = '(<number>)'
答案 0 :(得分:0)
我需要移动我在递归CTE and Rekey = '(<number>)'
之外使用的条件。这样做使得递归CTE立即返回正确的结果。在递归CTE之外的条件意味着递归CTE正在为整个表中的每个数字执行此递归。