21

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?

2 Answers 2

21

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.

5
  • Can this materialized view be made to update immediately on source views changing? Is that what 'fast refresh' does?
    – Clinton
    Jun 30, 2011 at 8:05
  • 5
    @Clinton - "Fast refresh" means that the data is updated incrementally. "Refresh on commit" in a materialized view definition means that the data is updated when the source table changes are committed-- the refresh is synchronous with the commit. Refresh on commit requires a fast refresh but a fast refresh does not imply refresh on commit. Jun 30, 2011 at 14:16
  • @Justin, thank you for the clarification. OP needs the "Refresh on commit" but now you got me wondering what an incremental update is. I assumed that, like SQL Server, a select from a materialized view would always return the same results as the select that created the view. Was my assumption wrong? Jun 30, 2011 at 14:29
  • 7
    An incremental update means that only the changes made to the underlying table are applied to refresh the MV rather than re-executing the entire query to refresh the MV. This requires that MV logs exist on the base tables and that the MV be sufficiently simple to allow incremental updates. It is entirely possible for a query against a MV to return different results than a query against the base tables. Frequently, for example, MVs are used to replicate data from one system to another where there is a lag between changes being made at the source and appearing at the destination. Jun 30, 2011 at 14:42
  • @Justin - You should make an answer out of this so I can delete mine. You have my vote. Jun 30, 2011 at 17:50
10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.