SQL-Server Performance: What is faster, a stored procedure or a view?

后端 未结 11 697
星月不相逢
星月不相逢 2020-12-04 23:53

What is faster in SQL Server 2005/2008, a Stored Procedure or a View?

EDIT: As many of you pointed out, I am being too vague. Let me attempt to

11条回答
  •  星月不相逢
    2020-12-05 00:21

    Stored Procedures (SPs) and SQL Views are different "beasts" as stated several times in this post.

    If we exclude some [typically minor, except for fringe cases] performance considerations associated with the caching of the query plan, the time associated with binding to a Stored Procedure and such, the two approaches are on the whole equivalent, performance-wise. However...

    A view is limited to whatever can be expressed in a single SELECT statement (well, possibly with CTEs and a few other tricks), but in general, a view is tied to declarative forms of queries. A stored procedure on the other can use various procedural type constructs (as well as declarative ones), and as a result, using SPs, one can hand-craft a way of solving a given query which may be more efficient than what SQL-Server's query optimizer may have done (on the basis of a single declarative query). In these cases, an SPs may be much faster (but beware... the optimizer is quite smart, and it doesn't take much to make an SP much slower than the equivalent view.)

    Aside from these performance considerations, the SPs are more versatile and allow a broader range of inquiries and actions than the views.

提交回复
热议问题