SQL Server – Does Server Calculate Views on Each Query?

sql serverview

I wonder whether sql server calculate views every time when someone query the views. It is because everytime I query the views, it take such a long time.

And when I tried to shorten the time by selecting only top 10 rows from the view, it takes the same amount of time. I suspect sql server need to calculate the view for the whole table even if I only want a few rows from the view, am I right?

Best Answer

Yes, a view is just a macro (unless indexed). The behaviour is well known.

See Tony Rogerson's article. You can see it yourself in the query plans too. The view doesn't exist on the query plan because it can't exist as a macro

Also note the "Predicate Pushing" phenomenon when you filter or TOP on views