Advantages of Indexed Views in SQL Server

By: Dusan Petkovic


Besides possible performance gains for complex views that are frequently referenced in queries, the use of indexed views has two other advantages:

  • The index of a view can be used even if the view is not explicitly referenced in the FROM clause.
  • All modifications to data are reflected in the corresponding indexed view.

Probably the most important property of indexed views is that a query does not have to explicitly reference a view to use the index on that view. In other words, if the query contains references to columns in the base table(s) that also exist in the indexed views, and the optimizer estimates that using the indexed view is the best choice, it chooses the view indices in the same way it chooses table indices when they are not directly referenced in a query.

When you create an indexed view, the result set of the view (at the time the index is created) is stored on the disk. Therefore, all data that is modified in the base table(s) will also be modified in the corresponding result set of the indexed view.

Besides all the benefits that you can gain by using indexed views, there is also a (possible) disadvantage: indices on indexed views are usually more complex to maintain than indices on base tables, because the structure of a unique clustered index on an indexed view is more complex than a structure of the corresponding index on a base table.

The following types of queries can achieve significant performance benefits if a view that is referenced by the corresponding query is indexed:

  • Queries that process many rows and contain join operations or aggregate functions
  • Join operations and aggregate functions that are frequently performed by one or several queries

If a query references a standard view and the database system has to process many rows using the join operation, the optimizer will usually use a suboptimal join method. However, if you define a clustered index on that view, the performance of the query could be significantly enhanced, because the optimizer can use an appropriate method. (The same is true for aggregate functions.)

If a query that references a standard view does not process many rows, the use of an indexed view could still be beneficial if the query is used very frequently. (The same is true for groups of queries that join the same tables or use the same type of aggregates.)

NOTE

Since SQL Server 2008 R2, Microsoft offers an alternative solution to indexed views called filtered indices. Filtered indices are a special form of nonclustered indices, where the index is narrowed using a condition in the particular query. Using a filtered index has several advantages over using an indexed view.

Comments

Leave a Reply