CTE是最好的或临时表是最好的 [英] CTE is best or temp table is best

查看:129
本文介绍了CTE是最好的或临时表是最好的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,我们不能在CTE.So上创建索引。因此,临时表是最好的,因为我们可以在临时表上创建索引。



但是另一方面,我们总是说CTE更适合比较临时的表格吗?



如果在SQL服务器中使用CTE,它不会影响性能吗?



我尝试过:



SQL中的CTE性能Serevr

As we know,we can not create indexes on CTE.So as per this , temporary tables are best because we can create indexes on temp table.

but on other hand we always say that CTE are to be preferred comparatively temp tables?

Isn't it effect performance if use CTE in SQL server

What I have tried:

Performance of CTE in SQL Serevr

推荐答案

没有办法说明哪种方法可以更好地获取临时数据,尤其是在没有特定要求的情况下。这取决于你想要达到的目标。很明显,当您需要使用递归获取数据时,会使用CTE。



详情请见:

sql server - 哪些性能更​​高,CTE还是临时表? - 堆栈溢出 [ ^ ]

sql server 2014 - CTE,临时表和表变量 - 堆栈溢出 [ ^ ]
There's no way to say which method to grab temporary data is better, especially when there's no specific requirements. It depends on what you're trying to achieve. It's obvious that CTE is used when you need to fetch data using recursion.

For further details, please see:
sql server - Which are more performant, CTE or temporary tables? - Stack Overflow[^]
sql server 2014 - CTE,Temp table and table variable - Stack Overflow[^]


临时表是tempdb中的真实对象,但cte只是复杂查询的一种包装,简化了组织递归的语法一步。



CTE是无法索引的,临时表可以被索引

CTE不能有约束,临时表可以有约束


您应该使用#Temp Tables进行更长时间的查询,使用CTE进行小数据集
Temp table is a real object in tempdb, but cte is only a kind of wrapper around complex query to simplify syntax of organize recursion in one step.

CTEs are unindexable and temp tables can be indexed
CTEs cannot have constraints and temp tables can have constraints

You should use #Temp Tables for longer queries and CTEs for small dataset


这篇关于CTE是最好的或临时表是最好的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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