Skip to Main Content
  • Questions
  • Index-Organized Materialized View with different primary key than the master table?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Walter.

Asked: June 14, 2018 - 10:08 am UTC

Last updated: June 19, 2018 - 3:20 am UTC

Version: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

Viewed 1000+ times

You Asked

Dear Oracle-Team,

we need a daily snapshot from the company's personal data for our software.
For that reason we want to use an index-organized materialized view (with daily 'refesh complete'). Unfortunately there are two user id's for every employee in the company. The primary key in the master table is the USERID_A. But for our software we are only using USERID_B (see LiveSQL Link - Step 1-6). But because that is not the primary key oracle perform an 'INDEX FAST FULL SCAN' for that search (Step 7, 8). Of cource I can add an index for the USERID_B but than I will miss the performance advantage of an index-organized materialized view (Step 9-11).

Summary:
* we only need an 'refesh complete'
* we need the mv read-only
* we want to have performance advantages from an index-organized materialized view

Question: With the summary points in mind, can I create an mv with an different primary key than the master table?

Thank you in advance for your expertise!

Greetings, Walter

PS: For sure I can fix this problem also on an other way (IOT with an job), but the way with an index-organized materialized view seems to be much more elegent.

with LiveSQL Test Case:

and Connor said...

Definitely - you can use the 'prebuilt' syntax

SQL> create table t as
  2    select
  3      object_id userid1,
  4      object_id userid2,
  5      owner,
  6      object_name
  7  from dba_objects
  8  where object_id is not null;

Table created.

SQL>
SQL> alter table t add primary key ( userid1);

Table altered.

SQL>
SQL> create table t_mv ( userid1, userid2, owner, object_name, constraint t_mv_pk primary key ( userid2)   )
  2  organization index as select * from t;

Table created.

SQL> create materialized view t_mv
  2  on prebuilt table
  3  refresh complete on demand
  4  as select * from t ;

Materialized view created.

SQL> exec dbms_mview.refresh('T_MV')

PL/SQL procedure successfully completed.



Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, June 18, 2018 - 10:25 am UTC

Thank you very much for the great instructions!
I looked for this solution.

One think I found out is that I don't even need the bold statement in your suggestion -
create table t_mv ( userid1, userid2, owner, object_name, constraint t_mv_pk primary key ( userid2)   )
  organization index <b>as select * from t</b>


I just have to specify the fields in the CREATE TABLE statement - e.g.
create table t_mc
(
  userid1 number,
  userid2 number,
  Details varchar2(100),
  constraint t_mv_pk primary key ( userid2)
)

That makes the code easier to maintain, when the SELECT statement is more extensive.

Greetings, Walter

Connor McDonald
June 19, 2018 - 3:20 am UTC

glad we could help

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.