Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index on view (Oracle)

Tags:

sql

oracle

Lets say I have two tables, tab_a and tab_b.

And I create a view like the following:

create view join_tabs as
(
  select col_x as col_z from tab_a
  union
  select col_y as col_z from tab_b
);

And if I do the following:

select * from join_tabs where col_z = 'BLAH';

If tab_a indexes col_x and tab_b indexes col_y, we should be able to do this with two index searches.

However, it would be nice if I could make an index over both tables in one index, or even index the view, in a way that automatically updates immediately if the source tables (tab_a or tab_b) change.

Is there a way to do this in Oracle?

like image 432
Clinton Avatar asked Jun 30 '11 07:06

Clinton


People also ask

Can Oracle View have index?

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.

Can you put an index on a view?

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.


2 Answers

I'm not up-to-par with Oracle but I believe Materialized Views do exactly that.

Choosing Indexes for Materialized Views

The two most common operations on a materialized view are query execution and fast refresh, and each operation has different performance requirements. Query execution might need to access any subset of the materialized view key columns, and might need to join and aggregate over a subset of those columns. Consequently, query execution usually performs best if a single-column bitmap index is defined on each materialized view key column.

In the case of materialized views containing only joins using fast refresh, Oracle recommends that indexes be created on the columns that contain the rowids to improve the performance of the refresh operation.

If a materialized view using aggregates is fast refreshable, then an index is automatically created unless USING NO INDEX is specified in the CREATE MATERIALIZED VIEW statement.

like image 86
Lieven Keersmaekers Avatar answered Oct 26 '22 11:10

Lieven Keersmaekers


You cannot create an index on a view, since a view is merely a mask on some tables(s). To do so, create a materialized view as specified by @Lieven and create an index on it.

like image 21
Hasan Fahim Avatar answered Oct 26 '22 10:10

Hasan Fahim