Indexed views are a new concept introduced in SQL Server 2000. They allow view resultsets to be stored in the database's physical storage after an index is created. In contrast to this, in a non-indexed view, the view is activated at run time and the resultset is dynamically built based on logic needed to create output from the under-lying base tables. Any computations , such as joins or aggregations, are done during query execution for each query referencing the view, ultimately leading in degraded performance. Indexed views and other views are covered in full in Chapter 10. A view can be thought of as a virtual table or a stored query. The results of using a view are not permanently stored in the database. Defining clustered indexes on views enables the resultsets of the view to be stored in the database, thereby reducing the overhead of dynamically building the resultset. This means that indexed views can be treated like any other table because additional nonclustered indexes can be added. An indexed view can be created using the CREATE INDEX or CREATE VIEW statements, which is done when data is occasionally updated. Some of the benefits of indexed views are as follows :
Although benefits can be had from applying an index to a view, there are many considerations to be made that may affect other applications as well. Indexed View Requirements and RestrictionsNOTE Index View Optimizer Restrictions The optimizer uses indexed views in only the Enterprise edition, although they can only be created in both the SQL Server 2000 Professional and Enterprise editions. Some prerequisites need to be met before you create an indexed view, as follows:
NOTE Switching Session-Level Settings One way of turning session settings on or off is to use the session property statement. For example, to switch the ANSI_WARNINGS setting on, you can run the following query IF sessionproperty ('ANSI_WARNINGS') = 0 SET ANSI WARNINGS ON These session settings must be on during the creation of the indexed view, as well as for the clients who wish to use the indexed view. To avoid having problems with indexed views, keep the restrictions in mind, but also be aware that options must also be in place at the time the indexed view is created. When creating indexed views, keep the following points in mind:
With all this in mind, there is a lot to consider about the specifics of an implementation of indexed views. Creating Indexed ViewsCreating an indexed view is as simple as altering the CREATE INDEX statement. Use the same syntax used in creating table indexes except that the table name is replaced with the view name. Also, some session-level options must be toggled (see the preceding section). Keep in mind that only the view owner is allowed to execute a CREATE INDEX statement on a view. To create an indexed view, go through Step by Step 10.9.
All indexes on a view are dropped if the view is dropped. All nonclustered indexes on the view are dropped if the clustered index is dropped. Nonclustered indexes can be dropped individually. Dropping the clustered index on the view removes the stored result set, and the optimizer returns to processing the view like a standard view. REVIEW BREAK: Using Indexed ViewsIndexed views work best when the underlying data is static or infrequently updated. The maintenance of an indexed view can be higher than the cost of maintaining a table index. If the underlying data is updated frequently, then the cost of maintaining the indexed view data may outweigh the performance benefits of using the indexed view. Indexed views are best used in reporting systems with many joins, functions, aggregations, and other calculations. Any time data is to be summarized, grouped, or otherwise analyzed , indexed views will be of benefit. Indexed views are best when there are many or very expensive selects compared to the number of inserts , updates, and deletes. |