patternsqlModerate
Are Views optimized when I add a WHERE clause to them?
Viewed 0 times
arewhereoptimizedviewswhenthemclauseadd
Problem
Does it make a difference if you filter a View inside or outside the View?
For example, is there any difference between these two queries?
Or
And
And is the answer any different if the source table is located on a Linked Server?
I'm asking because I have to query a large table (44mil rows) twice from a linked server, and get an aggregate of the results. I want to know if I should create two views to access the data, one for each query, or if I can get away with a single view and a
For example, is there any difference between these two queries?
SELECT Id
FROM MyTable
WHERE SomeColumn = 1Or
SELECT Id
FROM MyView
WHERE SomeColumn = 1And
MyView is defined asSELECT Id, SomeColumn
FROM MyTableAnd is the answer any different if the source table is located on a Linked Server?
I'm asking because I have to query a large table (44mil rows) twice from a linked server, and get an aggregate of the results. I want to know if I should create two views to access the data, one for each query, or if I can get away with a single view and a
WHERE clause.Solution
You should see absolutely no difference in the plans or the performance between these two choices. When the view is queried, it is expanded out to a query against the base table, which means the same seek or scan will be used.
Now, depending on the data type and selectivity of
Now, depending on the data type and selectivity of
MyColumn, if you wanted to create a filtered index on the base table (when you move to SQL Server 2008+), you might get better performance, but this again will not be different via the view or without.Context
StackExchange Database Administrators Q#15363, answer score: 17
Revisions (0)
No revisions yet.