So based off the way I've seen WITH
used, and the documentation at MSDN :
Specifies a temporary named result set, known as a common table expression (CTE).
it appears that the WITH
is the replacement for #TEMP
tables. Is that correct?
So based off the way I've seen WITH
used, and the documentation at MSDN :
Specifies a temporary named result set, known as a common table expression (CTE).
it appears that the WITH
is the replacement for #TEMP
tables. Is that correct?
3条答案
按热度按时间carvr3hs1#
No. CTEs—introduced by
WITH
—don't replace temp tables, although in some cases they can be used where one might have used a temp table in the past.WITH
is really nothing more than a derived table, with the difference that it is introduced before the query instead of inline, and is given an alias which can then be used as a table throughout the query multiple times.A derived table is a complete query, inside of parentheses, that is used as if it were a real table. Views and table-valued functions are also considered derived tables, but we're focusing on the kind that is defined inline. Here is an example:
We have a completely intact query that returns its own rowset (the
GROUP BY
query). By placing this inside of parentheses and assigning it an aliasS
, we can now use it like a table. We could join more tables to this one. But, we have only joined to this table once.To convert this to a CTE, we make a very simple change:
Now, a temp table is a completely different animal. It has very important differences from a CTE or derived table:
Avg(SalesTotal)
calculation, in versions of SQL Server through at least 2012, will involve a completely separate operation of performing theSalesTotals
aggregate a second time. While it is possible for the engine to materialize the results of the CTE, so far SQL Server has not done this. It is notable that other DBMSes such as Oracle may materialize the results of a CTE. In any case, you should be aware that this double-querying can have (of course!) serious performance implications.Last, a CTE can do something a temp table cannot: it can be recursive. In Oracle this is expressed through
CONNECT BY
, and in SQL Server it is done with aUNION ALL SELECT
inside the CTE that is allowed to refer to the CTE's own alias.Be careful with CTEs. They are a great abstraction, but are nothing more than that, and you can run into serious trouble with them. Generating a million rows can be done with a recursive CTE one row at a time, but it's the WORST possible way by like a hundred times over or more.
There is another special kind of temp table in SQL Server 2005 and up called a "table variable" that is very much like a temp table (and kept in tempdb exactly the same), with a few notable exceptions:
mccptt672#
SQL优化器现在在选择最佳执行计划方面做得更好,但是当连接10个以上的表时,特别是连接一些大型表和视图并需要使用多个过滤器时,它通常不能以最佳方式执行。我仍然发现没有什么比使用#TEMP表并在将查询连接在一起之前将它们分解成更小的子集更快的了。注意:我很少发现向#TEMP表添加索引可以提高性能。
mec1mxoz3#
不,这是不正确的。它们都是独立的功能,并且各自有各自的用途。
例如,CTE适用于小数据位,而临时表通常更适合于较大的数据集。临时表可以被索引,其性能可以得到改善,而CTE则不行。
我会花一点时间阅读MSDN文档,并查看您将使用其中一个的特定示例