We would like to be able to use fast refresh on a materialised view which contains a union.
This has worked when the union uses the same table. However it does not seem to work with a different table name even though the primary key, and columns selected are identical. We are using Oracle9i Enterprise Edition Release 9.2.0.1.0.
The example that worked:
Table name QH_PAT.
Name Null? Type
------------------------------- -------- ----
FCLTY_ID NOT NULL VARCHAR2(6)
PAT_ID NOT NULL VARCHAR2(8)
SEX NOT NULL VARCHAR2(6)
BIRTH_DATE NOT NULL DATE
BIRTH_DATE_EST_CODE VARCHAR2(6)
VALID_FROM NOT NULL DATE
VALID_TO NOT NULL DATE
FCLTY_UNIQUE_ID NOT NULL VARCHAR2(12)
Primary Key:
alter table qh_pat
add constraint qh_pat_pk
primary key
(
fclty_id,
fclty_unique_id
);
Snapshot log script:
create snapshot log on QH_PAT
with primary key
Materialized View script:
create snapshot qh_pat_mv as
select fclty_id,
fclty_unique_id,
pat_id,
sex,
birth_date_est_code
from qh_pat
where sex = 1
union
select fclty_id,
fclty_unique_id,
pat_id,
sex,
birth_date_est_code
from qh_ pat
where sex = 2
/
The example that creates a complex materialized view that will not allow fast refresh:
Table Name: QH_WORK_PAT
FCLTY_ID NOT NULL VARCHAR2(6)
PAT_ID NOT NULL VARCHAR2(8)
SEX VARCHAR2(6)
BIRTH_DATE VARCHAR2(8)
BIRTH_DATE_EST_CODE VARCHAR2(6)
VALID_FROM NOT NULL VARCHAR2(12)
VALID_TO VARCHAR2(12)
FCLTY_UNIQUE_ID NOT NULL VARCHAR2(12)
Primary Key:
alter table qh_work_pat
add constraint qh_work_pat_pk
primary key
(
fclty_id,
fclty_unique_id
);
Snapshot log script:
create snapshot log on QH_WORK_PAT
with primary key
Materialized View Script:
create snapshot qh_pat_mv as
select fclty_id,
fclty_unique_id,
pat_id,
sex,
birth_date_est_code
from qh_pat
union
select fclty_id,
fclty_unique_id,
pat_id,
sex,
birth_date_est_code
from qh_work_pat
In the first example using QH_PAT only, I can include refresh fast with primary key in the script. This also is ok if QH_WORK_PAT is used for both queries.
In the second example using QH_PAT and QH_WORK_PAT. If I put refresh fast with primary key in the create script I get the following error.
Creating snapshot QH_PAT_MV
qh_work_pat
*
ERROR at line 18:
ORA-12015: cannot create a fast refresh materialized view from a complex query
According to the 9i documentation a union does not make a materialized view complex unless one of the defining queries is complex. I can not see any mention of having different table names as a cause of complex.
Have you any ideas.
Can you use a UNION ALL instead of a UNION?
UNION ALLS are supported.
A union B is like:
distinct (sort ( A + B ))
A union all B is like:
A + B
if you do not need the sort/distinct, UNION ALL is the way to go. As you can imagine, trying to do a fast refresh from a UNION view of two tables would be very very hard. Suppose you just have two tables with one column each and one row each. Each has a row with X=1. So,
a union b = { 1 }
just one tuple. Now, you delete from B. How do you apply that ? That delete should have no net effect on the materialized view -- but you don't know that unless you reprocess the entire A union B.
with a UNION ALL -- it would be possible but you'll need to add some maintenance columns:
ops$tkyte@ORA920.US.ORACLE.COM> create table t1 ( x int primary key, y int );
Table created.
ops$tkyte@ORA920.US.ORACLE.COM> create table t2 ( x int primary key, y int );
Table created.
ops$tkyte@ORA920.US.ORACLE.COM>
ops$tkyte@ORA920.US.ORACLE.COM> create snapshot log on t1 with primary key, rowid;
Materialized view log created.
ops$tkyte@ORA920.US.ORACLE.COM> create snapshot log on t2 with primary key, rowid;
Materialized view log created.
ops$tkyte@ORA920.US.ORACLE.COM>
ops$tkyte@ORA920.US.ORACLE.COM> create materialized view mv2
2 refresh fast
3 as
4 select x, y from t1
5 union
6 select x, y from t2
7 /
select x, y from t2
*
ERROR at line 6:
ORA-12015: cannot create a fast refresh materialized view from a complex query
ops$tkyte@ORA920.US.ORACLE.COM>
ops$tkyte@ORA920.US.ORACLE.COM> create materialized view mv2
2 refresh fast
3 as
4 select x, y, 1 umarker , rowid rid from t1
5 union ALL
6 select x, y, 2 umarker , rowid rid from t2
7 /
Materialized view created.
See
</code>
http://docs.oracle.com/cd/B10501_01/server.920/a96520/mv.htm#574889 <code>
for the details on this
new 9iR2 feature -- you cannot do UNION ALL mv's before that release