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

后端 未结 11 657
星月不相逢
星月不相逢 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:26

    In short, based on my experience in some complex queries, Stored procedure gives better performance than function.

    But you cannot use results of stored procedure in select or join queries.

    If you don't want to use the result set in another query, better to use SP.

    And rest of the details and differences are mentioned by people in this forum and elsewhere.

    0 讨论(0)
  • 2020-12-05 00:30

    Unfortunately, they're not the same type of beast.

    A stored procedure is a set of T-SQL statements, and CAN return data. It can perform all kinds of logic, and doesn't necessarily return data in a resultset.

    A view is a representation of data. It's mostly used as an abstraction of one or more tables with underlying joins. It's always a resultset of zero, one or many rows.

    I suspect your question is more along the lines of:

    Which is faster: SELECTing from a view, or the equivalent SELECT statement in a stored procedure, given the same base tables performing the joins with the same where clauses?

    0 讨论(0)
  • 2020-12-05 00:31

    This isn't really an answerable question in that an answer will hold true in all cases. However, as a general answer for an SQL Server specific implementaion...

    In general, a Stored Procedure stands a good chance of being faster than a direct SQL statement because the server does all sorts of optimizations when a stored procedure is saves and executed the first time.

    A view is essentially a saved SQL statement.

    Therefore, I would say that in general, a stored procedure will be likely to be faster than a view IF the SQL statement for each is the same, and IF the SQL statement can benefit from optimizations. Otherwise, in general, they would be similar in performance.

    Reference these links documentation supporting my answer.

    http://www.sql-server-performance.com/tips/stored_procedures_p1.aspx

    http://msdn.microsoft.com/en-us/library/ms998577.aspx

    Also, if you're looking for all the ways to optimize performance on SQL Server, the second link above is a good place to start.

    0 讨论(0)
  • 2020-12-05 00:32

    Found a detailed performance analysis: https://www.scarydba.com/2016/11/01/stored-procedures-not-faster-views/

    Compile Time Comparison:

    There is a difference in the compile time between the view by itself and the stored procedures (they were almost identical). Let’s look at performance over a few thousand executions:

    View AVG: 210.431431431431

    Stored Proc w/ View AVG: 190.641641641642

    Stored Proc AVG: 200.171171171171

    This is measured in microsends, so the variation we’re seeing is likely just some disparity on I/O, CPU or something else since the differences are trivial at 10mc or 5%.

    What about execution time including compile time, since there is a difference:

    Query duration View AVG: 10089.3226452906

    Stored Proc AVG: 9314.38877755511

    Stored Proc w/ View AVG: 9938.05410821643

    Conclusion:

    With the exception of the differences in compile time, we see that views actually perform exactly the same as stored procedures, if the query in question is the same.

    0 讨论(0)
  • 2020-12-05 00:39

    Views:

    • We can create index on views (not possible in stored proc)
    • it's easy to give abstract views(only limited column access of multiple table ) of table data to other DBA/users

    Store Procedure:

    • We can pass parameters to sp(not possible in views)
    • Execute multiple statement inside procedure (like insert, update,delete operations)
    0 讨论(0)
提交回复
热议问题