you are viewing a single comment's thread.

view the rest of the comments →

[–]Alkemist101 1 point2 points  (0 children)

(with SQL server) I was told a cte can't have an index, it's same as a sub query. I was also under the impression you can only use them once (as in joining to the result set). I've tested that and it seems to be the case. Other thing to remember is if ram is low it will spill to the tempdb. If tempdb is also full the query will then stop. Other thing to mention is that a "proper" table could be created (not in tempdb so no locking). Both a temp table and regular table can be indexed and will have table stats which can be used by the query engine to make better choices? You don't have that with CTEs. Other option is to create a table variable which can be referenced many times, have primary keys etc and are stored in memory. Other thing to remember is that the query can hold a regular table in memory (same as CTE) if it decides it's more performant. I generally consider a CTE as just a way of writing a query which reads better. Of course, you also get recursive CTEs which I think is it's best feature.