Hey folks,
In the past, one of the factors I'd consider when choosing to use a Stored Procedure over a View was the fact that the Stored Procedure would get optimized by storing the query execution path (I'm a developer so I understand this at a higher level than a DBA would). But I've recently become aware of the fact that you can now Index your Views. This has now raised new questions for me as to when I'd get better performance out of the Indexed View versus the Stored Procedure?
Take for example the following:
SELECT colA, colB, sum(colC), sum(colD), colE
FROM myTable
WHERE colFDate < '9/30/2011'
GROUP BY colA, colB, colE
The date will be different every time it's run, so if this were a view, I wouldn't include the WHERE in the view and instead have that as part of my select against the View. If it were a stored procedure, the date would be a parameter.
If this were an Indexed View, should I expect to get better performance out of it then a stored procedure that's had an opportunity to cache the execution path? Or would the proc be faster? Or would
the difference be negligible? I know we could say "just try both out" but there are too many factors that could falsely bias the results, so I'd like to hear more of the theory behind it and what the expected outcomes are instead.
Thanks!