Simple view question regarding indexes

  • Do views take advantage of the underlying tables' indexes or does one need to add an index with the view being schema bound?

  • 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.

  • Thanx. So, what's the advantage of adding an index to a view?

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanx.

  • 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.

  • Thanx.

  • 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.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • 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?

  • 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.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • 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