WITH Common Table Expression vs. CREATE VIEW performance

你离开我真会死。 提交于 2020-01-01 15:52:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!