invisibleduncan (4/2/2013)
So although it could be argued that I was correct, any query that uses the view would create a cached execution plan in the same way that it would if it was querying from a table?
Yes, because by the time that the query reaches the optimiser, there's no reference to the view remaining, during the parsing and binding, the names of views will be replaced by their definitions (unless we're talking about indexed views)
For example:
CREATE VIEW SomeView AS
SELECT Col1, Col2 FROM SomeTable WHERE Col3 IS NOT NULL
GO
Now we have a query that uses that view
SELECT Col1, SUM(Col2) FROM SomeView GROUP BY Col1
So during the parsing/binding, the view name is replaced by its definition, resulting in
SELECT Col1, SUM(Col2) FROM (SELECT Col1, Col2 FROM SomeTable WHERE Col3 IS NOT NULL) sv GROUP BY Col1
Which is then simplified and optimised from there and the resulting execution plan cached as per the usual rules for caching queries.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability