Views vs. inline subqueries SQL Server 2005/2008

烂漫一生 提交于 2019-12-22 00:25:30

问题


In certain areas in my application I need data from several tables in the database (I have an application, the application has many attributes, each attribute has several definitions, and each definition has a value). I need the data from all these tables for an application. Should I use a view (one that would be rather large) or subqueries for selecting the records? In the case of subqueries is the optimizer able to work efficiently? I'm also interested if caching will work for subqueries.


回答1:


Views are typically expanded in place into subqueries, unless you explicitly mark the views as persisted by dropping a clustered index on them.




回答2:


This is an 'It depends' question. A view might help to make the code more maintainable but complex selection predicates might confuse the optimiser.

Another option is a stored procedure that returns a record set. If you reuse a subquery several times you may get some mileage from splitting up the query, selecting the subquery into a temporary table and combining the parts in a later step.

Without a more specific description of the problem it's hard to really give a meaningful answer.



来源:https://stackoverflow.com/questions/181949/views-vs-inline-subqueries-sql-server-2005-2008

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