April 17, 2014 at 5:36 am
Do views take advantage of the underlying tables' indexes or does one need to add an index with the view being schema bound?
April 17, 2014 at 6:23 am
Yes, the queries used to define views will take advantage of the indexes on the underlying tables if they assist in the return of data from those tables.
April 17, 2014 at 7:32 am
Thanx. So, what's the advantage of adding an index to a view?
April 17, 2014 at 7:37 am
If the view has an index, SQL may not need to go to the base tables at all.
Bear in mind there are a massive set of restrictions around indexed views. Not all views can be indexes.
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
April 17, 2014 at 9:35 am
Thanx.
April 17, 2014 at 2:42 pm
NineIron (4/17/2014)
Thanx. So, what's the advantage of adding an index to a view?
When you access a non-indexed view, SQL will expand the query that defines the view to access the data that is needed. Let's say you have a view with an aggregation in it. SQL will chug through and re-calculate the aggregation every time you access the view. If you index the view, the aggregation may not need to be recalculated every time.
There are lots of caveats and drawbacks too, but that is one use case.
April 18, 2014 at 4:51 am
Thanx.
April 18, 2014 at 5:52 am
FYI: Indexed Views don't do well on high-volume insert tables.
I also seem to recall that trying to update the schema of underlying tables used in Indexed Views (WITH SCHEMABINDING) is nigh-on impossible without first dropping the view.
April 18, 2014 at 6:20 am
Thanx. My environment is basically reporting off of a DataWarehouse so, performance of the report/query is most important. Could I ask..........if the underlying table(s) is not indexed or poorly indexed (I have little control over this), will an indexed view work to speed up performance?
April 18, 2014 at 6:25 am
NineIron (4/18/2014)
if the underlying table(s) is not indexed or poorly indexed (I have little control over this), will an indexed view work to speed up performance?
Yes and no. The problem is that an indexed view uses cached data. So in that respect, yes. But when you have to refresh that data, it's still relying on the underlying tables. Plus it depends on what indexes you put in the view. There are lots of ways to screw up an index and make a query worse. I've done about half of them over the years. @=)
Of course, the best way to know the answer to any "performance saving solution" for sure is to test this in a Dev or sandbox server. See for yourself.
April 18, 2014 at 6:30 am
Thanx. Time to bring out the pail and shovel
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply