Background
Hello all!
I recently learned that in newer versions of SQL Server, the query optimizer can "expand" a SQL view and utilize inline performance benefits. This could have some drastic effects going forward on what kinds of database objects I create and why and when I create them, depending upon when this enhanced performance is achieved and when it is not.
For instance, I would not bother creating a parameterized inline table-valued function with a start date parameter and an end date parameter for an extremely large transaction table (where performance matters greatly) when I can just make a view and slap a WHERE statement at the bottom of the calling query, something like
SELECT
Column1
FROM vw_Simple
WHERE
Column1 BETWEEN @SomeStartDate AND @SomeEndDate
and trust that the query optimizer will "expand" the view and give me a great execution plan.
Note: I am talking about a simple, non-nested, non-indexed SQL Server view. Something like
CREATE VIEW vw_Simple
AS
SELECT
Column1
,Column2
FROM TableA
Question
My question is: what are the exact guidelines for knowing when the query optimizer can "expand" a SQL view and when it cannot?
I cannot find this answer in the official Microsoft documentation.
What I've found so far
Cases where the query optimizer can expand a view:
- This Stack Exchange post claims that, in general, the query optimizer will expand a SQL view.
Cases where the query optimizer cannot expand a view:
- Predicate Pushing/Deferred Filtering - but this was addressed in SQL Server 2008.
- Nested Views - at least sometimes.
Gray Area
- This Stack Overflow post's second answer claims that the query optimizer may or may not expand the view, depending on the view's complexity and the limitations of the query optimizer.