Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do table indexes come into play when using a view?

I have a view that collects data from several tables. While there are no indexes on the view itself anything that uses the view seems to benefit from the underlying tables having indexes. Are these being used automatically? If they are then what is the point of creating indexes on your views? Any recommended articles on this subject would be welcomed.

like image 368
Abe Miessler Avatar asked Dec 17 '09 18:12

Abe Miessler


People also ask

Do indexes work on views?

Indexes can only be created on views which have the same owner as the referenced table or tables. This is also called an intact ownership-chain between the view and the table(s). Typically, when table and view reside within the same schema, the same schema-owner applies to all objects within the schema.

What happens when 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.

Does view Use index base table?

The indexes on the base table are still used whenever you access the view. You don't need to use an indexed view, unless the view contains an expensive logic (aggregations or joins) that you don't want to perform each time you query the view.

Can a view have indexes in Oracle?

Oracle SQL standards do not support creating indexes on views. If you need to index documents whose contents are in different tables, you can create a data storage preference using the USER_DATASTORE object.


2 Answers

Yes, the underlying table indexes are used automatically - a view just pulls the data from the underlying tables after all.

With regards to the benefits of creating indexes on a view, see this MS Technet article. Small excerpt:

Using indexes to improve query performance is not a new concept; however, indexed views provide additional performance benefits that cannot be achieved using standard indexes. Indexed views can increase query performance in the following ways:

  • Aggregations can be precomputed and stored in the index to minimize expensive computations during query execution.
  • Tables can be prejoined and the resulting data set stored.
  • Combinations of joins or aggregations can be stored.
like image 187
AdaTheDev Avatar answered Oct 05 '22 14:10

AdaTheDev


The query optimizer rewrites the query and "flattens" the use of sub-queries (which a view really is). So underlying indexes will be used.

like image 26
Alex Brasetvik Avatar answered Oct 05 '22 13:10

Alex Brasetvik