Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you want to put an index on a view?

Microsoft SQL Server allows you to add an index to a view, but why would you want to do this?

My understanding is that a view is really just a subquery, i.e., if I say SELECT * FROM myView, i'm really saying SELECT * FROM (myView's Query)

It seems like the indexes on the underlying tables would be the ones that matter the most. So why would you want a separate index on the view?

like image 707
Slider345 Avatar asked Jan 19 '11 15:01

Slider345


People also ask

Do you need index on view?

Conclusion. Indexes are great because they speed up the performance and with an index on a view it should really speed up the performance because the index is stored in the database. Indexing both views and tables is one of the most efficient ways to improve the performance of queries and applications using them.

When should you index a view?

Indexed views improve the performance of queries that use joins and aggregations in processing huge amount of data and are executed very frequently. The environments that are best suited to indexed views are data warehouses and the Online Analytical Processing (OLAP) databases.

What is the purpose of using an index?

Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.

What is the main reason to add an index to a table?

If you often search a table in Access or sort its records by a particular field, you can speed up these operations by creating an index for the field. Access uses indexes in a table as you use an index in a book: to find data, Access looks up the location of the data in the index.


2 Answers

If the view is indexed then any queries that can be answered using the index only will never need to refer to the underlying tables. This can lead to an enormous improvement in performance.

Essentially, the database engine is maintaining a "solved" version of the query (or, rather, the index of the query) as you update the underlying tables, then using that solved version rather than the original tables when possible.

Here is a good article in Database Journal.

like image 161
Larry Lustig Avatar answered Sep 22 '22 10:09

Larry Lustig


Microsoft SQL Server allows you to add an index to a view, but why would you want to do this?

To speed up the queries.

My understanding is that a view is really just a subquery, i.e., if I say SELECT * FROM myView, i'm really saying SELECT * FROM (myView's Query)

Not always.

By creating a clustered index on a view, you materialize the view, and updates to the underlying tables physically update the view. The queries against this view may or may not access the underlying tables.

Not all views can be indexed.

For instance, if you are using GROUP BY in a view, for it to be indexable it should contain a COUNT_BIG and all aggregate functions in it should distribute over UNION ALL (only SUM and COUNT_BIG actually are). This is required for the index to be maintainable and the update to the underlying tables could update the view in a timely fashion.

like image 43
Quassnoi Avatar answered Sep 24 '22 10:09

Quassnoi