大佬教程收集整理的这篇文章主要介绍了为什么CTE(递归)未并行化(MAXDOP = 8)?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我会尝试重写CTE以删除以下步骤之一,即
;cte as (
SELEct a.first_num, a.second_num, a.first_num as first_key, 1 as sequence_count
from T_SEQ_FF a where not exists (SELEct 1 from T_SEQ_FF b where a.first_num = b.second_num)
union all
SELEct a.first_num, a.second_num, cte.first_key, cte.sequence_count + 1
from T_SEQ_FF a
inner join cte on a.first_num = cte.second_num
)
SELEct *
from cte
option (maxrecursion 0);
如果只有一个根元素,最好将其作为变量传递到查询中,以便查询优化器可以使用该值。
另一尝试是更改查询以获取没有子查询的根元素,即,second_num为null或first_num = second_num。
我们有相当大的计算机100GB +内存和8+内核。服务器范围的MAXdop = 8。
T_SEQ_FF rowcount = 61692209,size = 2991152 KB
UPD 1: 表T_SEQ_FF
具有两个索引:
1) create index idx_1 on T_SEQ_FF (first_num)
2) create index idx_2 on T_SEQ_FF (second_num)
表格T_SEQ_FF
中的first_num
,second_num pairs
有num个,应在cte之后提供一个序列:
;with first_entity as (
SELEct first_num from T_SEQ_FF a where not exists (SELEct 1 from T_SEQ_FF b where a.first_num = b.second_num)
),cte as (
SELEct a.first_num,a.second_num,a.first_num as first_key,1 as sequence_count
from T_SEQ_FF a inner join first_entity b on a.first_num = b.first_num
union all
SELEct a.first_num,cte.first_key,cte.sequence_count + 1
from T_SEQ_FF a
inner join cte on a.first_num = cte.second_num
)
SELEct *
from cte
option (maxrecursion 0);
但是,当我运行此查询时,我只会看到没有并行的串行查询计划。如果我从上述查询中 删除 CTE的第二部分:
union all
SELEct a.first_num,cte.sequence_count + 1
from T_SEQ_FF a
inner join cte on a.first_num = cte.second_num
然后我可以看到使用Repartition和Gather Streams使查询计划成为 并行化 。
因此,我可以总结一下,这是因为 recurisve CTE的SQL Server处理此查询时,不使用并行。
我相信,在拥有大量免费资源的大型计算机上,并行性应有助于更快地完成查询。
现在,它运行约40-50分钟。
您能否建议如何使用尽可能多的资源来更快地完成查询?
CTE是唯一的选择,因为我们需要从first_num - second_num
成对中填充序列,并且这些序列可以是任何长度。
以上是大佬教程为你收集整理的为什么CTE(递归)未并行化(MAXDOP = 8)?全部内容,希望文章能够帮你解决为什么CTE(递归)未并行化(MAXDOP = 8)?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。