问题
I have several queries that use a WITH clause, or Common Table Expression, with a UNION ALL statement to recur through a table with a tree like structure in SQL server as described here. Would I see a difference in performance if I were to CREATE that same VIEW instead of including it with the WITH clause and having it generated every time I run the query? Would it generally be considered good practice to actually CREATE the view since it is used in several queries?
回答1:
What you're looking at is a Common Table Expression and not a View. If you're doing recursion, you need to stick with the CTE and not try to roll that into a view.
Views in MS SQL don't give you any performance benefits unless you are creating clustered indexes on them. From the sounds of your question, that is not the case. You'd likely be better off encapsulating your CTE inside of a stored procedure.
来源:https://stackoverflow.com/questions/1224623/with-common-table-expression-vs-create-view-performance