SQL Server 递归 CTE 是否被视为循环?

发布于 2024-12-10 16:58:18 字数 219 浏览 0 评论 0 原文

我的印象是递归 CTE 是基于设置的,但在 最近的 SO 帖子中 有人提到它们是循环。

递归 CTE 是否基于集合?我假设基于集合的操作不能是循环是错误的吗?

I was under the impression that recursive CTEs were set based, but in a recent SO post someone mentioned that they are loops.

Are recursive CTEs set based? Am I wrong to assume that a set based operation cannot be a loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

粉红×色少女 2024-12-17 16:58:18

如果它是递归的,它仍然被视为循环。
尽管一个语句是基于集合的,但一遍又一遍地调用它可以被视为循环。这是基于所使用的上下文的关于定义或措辞的争论。它们是基于集合的语句,但简单来说,处理被视为循环过程。

对于那些感兴趣的人来说,这里有一篇关于 CTE 性能的精彩小文章:

http://explainextended.com/2009/11/18/sql-server-are-the-recursive-ctes-really-set-based/

If it is recursive it is still considered a loop.
Although one statement is set based, calling it over and over can be considered a loop. This is an argument about the definition or wording based on the context being used. They are set based statements but the processing is considered in simple terms a looping process.

For those interested here is a nice little write up about performance with CTE's:

http://explainextended.com/2009/11/18/sql-server-are-the-recursive-ctes-really-set-based/

野却迷人 2024-12-17 16:58:18

它们是基于设置的。递归集仍然是集。

但如果你用足够强大的放大镜观察的话,所有的集合操作都是循环的。最终,代码在 CPU 上运行,CPU 执行在离散内存区域上运行的串行指令流。换句话说,不存在面向集合的硬件。 “面向集合”是一个逻辑概念。 所有 SQL 操作最终都是使用某种形式的循环来实现的,这一事实是一个实现细节。

They are set based. Recursive sets are still sets.

But all set operations are, if you look with a powerful enough magnifier glass, loops. Ultimately the code runs on CPUs and CPUs execute a stream of serial instructions that operate on discrete regions of memory. In other words, there is no set oriented hardware. Being 'set oriented' is a logical concept. The fact that all SQL operations are ultimately implemented using some form of a loop is an implementation detail.

时光病人 2024-12-17 16:58:18

我认为需要区分的是“尾递归”与“一般递归”。

所有尾递归都可以作为循环实现 - 不需要堆栈。

一般递归支持也可以实现为循环 - 但使用堆栈。

递归 CTE 是尾递归,因此本质上是一个循环。唯一的区别是终止条件由 SQL 语义/执行引擎处理。每个循环迭代的输出都是 UNIONed 或您指定的任何集合操作。

I think the distinction that needs to be done is "tail recursion" versus "general recursion".

All tail recursions can be implemented as loops - without the need for a Stack.

General recursion support can also be implemented as a Loop - but with a stack.

Recursive CTEs are Tail recursions and hence essentially a Loop. The only difference is the terminating condition is handled by SQL semantics/execution engine. The output from each Loop iteration is UNIONed or whatever set op you specify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文