Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

For which performace will be high. CTE or Temporary table in SQL server?

Kindly advise on this
Posted
Comments
Tomas Takac 8-Dec-15 4:18am    
Well, it depends. There is no "one size fits all" answer. It's up to you to tune the query. Try several combinations of CTE and temp tables and compare the performance.

They serve different purposes.
A CTE is stored in memory if it's materialized, and can only be used within one query.
The data in a temp table is persisted until it goes out of scope, and can therefore be used by many queries.
And a temp table can be indexed.

So the answer to your question would normally be that the CTE is at least as fast or faster, but the exception is when an indexed temp table makes a query so much faster that it compensates for the extra resources needed for persisting it.
 
Share this answer
 
You really need to research this properly ... it's sort of off-topic for this forum.

There is a discussion over at sqlservercentral[^] on the subject which also includes links to other articles.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900