CTE和SubQuery之间的区别? [英] Difference between CTE and SubQuery?

查看:59
本文介绍了CTE和SubQuery之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自此帖子如何在

有两种版本的答案,其中一种使用子查询,其他人使用 CTE 解决同一问题。

There are two versions of answers where one uses a sub-query and the other uses a CTE to solve the same problem.

现在,使用<$有什么好处c $ c> CTE(公用表表达式)通过子查询(因此,查询实际上在做什么 可读

Now then, what is the advantage of using a CTE (Common Table Expression) over a 'sub-query`(thus, more readable what the query is actually doing)

子选择 CTE 的唯一优势c>是实际上我可以命名 子查询。当将CTE用作简单(非递归)CTE时,这两个之间是否还有其他区别?

The only advantage of using a CTE over sub-select is that I can actually name the sub-query. Are there any other differences between those two when a CTE is used as a simple (non-recursive) CTE?

推荐答案

在子查询与简单(非递归)CTE版本中,它们可能非常相似。您将必须使用探查器和实际执行计划来发现任何差异,并且这将特定于您的设置(因此我们无法完全告诉您答案)。

In the sub-query vs simple (non-recursive) CTE versions, they are probably very similar. You would have to use the profiler and actual execution plan to spot any differences, and that would be specific to your setup (so we can't tell you the answer in full).

常规中; CTE可以递归使用;子查询不能。这使得它们特别适合于树形结构。

In general; A CTE can be used recursively; a sub-query cannot. This makes them especially well suited to tree structures.

这篇关于CTE和SubQuery之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆