2011-09-20 12 views
12

當我上運行的Oracle 10g以下代碼:甲骨文物化視圖錯誤:代碼中包含了

SQL Error: ORA-12053: this is not a valid nested materialized view 
12053. 00000 - "this is not a valid nested materialized view" 
*Cause: The list of objects in the FROM clause of the definition of this 
      materialized view had some dependencies upon each other. 
*Action: Refer to the documentation to see which types of nesting are valid. 

我不」:

drop materialized view test4; 
drop materialized view test3; 
drop table test2; 
drop table test1; 

create table test1 
(
    x1 varchar2(1000), 
    constraint test1_pk primary key (x1) 
); 

create materialized view log on test1 with sequence; 

create table test2 
(
    x2 varchar2(1000), 
    constraint test2_pk primary key (x2) 
); 

create materialized view log on test2 with sequence; 

create materialized view test3 
refresh complete on demand 
as 
(
    select x1 from test1 
    union all 
    select null from dual where 0 = 1 
); 

alter table test3 add constraint test3_pk primary key (x1); 

create materialized view log on test3 with sequence; 

create materialized view test4 
refresh fast on commit 
as 
(
    select t1.rowid as rid1, t2.rowid as rid2, t1.x1 u1, t2.x2 
    from test3 t1, test2 t2 
    where t1.x1 = t2.x2 
); 

我在試圖創建物化視圖test4得到這個錯誤瞭解「FROM子句」中的任何對象如何相互依賴。

我如何得到這個工作?目前我能想到的唯一工作是用普通表替換test3並手動刪除和刷新數據。這種方法很有效,但似乎有點破解。

或者(也許最好)我只想看一個可以有兩個表的例子,並將它們連接到一個物化視圖中,其中一個基表被批量更新(並且不需要反映在物化視圖中),但其他更新應反映在物化視圖中(即它是「一半」fast refresh on commit,和一半complete refresh on demand)。我嘗試使用refresh force,但是當使用EXECUTE DBMS_MVIEW.EXPLAIN_MVIEW()時,我發現沒有可用的提交刷新刷新的證據。我也想用union all來做到這一點。

+0

+1更好的解釋和現在ilustrated :) –

+0

'test3'沒有太大的意義:'從0選擇零對0 = 1'將永遠不會返回一行。 – Allan

+0

@Allan:這是一個黑客,使其成爲一個聚合。如果它停止了錯誤,請隨時刪除它。 – Clinton

回答

2

Oracle

Restrictions for Using Multitier Materialized Views

Both master materialized views and materialized views based on materialized views must:

  • Be primary key materialized views
  • Reside in a database that is at 9.0.1 or higher compatibility level

Note: The COMPATIBLE initialization parameter controls a database's compatibility level.

但是報價,我會盡力爲你解決。我會回來的。

更新:對不起,我沒有成功。你有太多的限制:)

+0

「太多限制」?你什麼意思?我的限制是什麼? – Clinton

+0

複雜的MV(所有工會),一個「批量刷新」(刷新完成)... –

0

你可能是出於運氣,每個Oracle文檔:

http://download.oracle.com/docs/cd/B28359_01/server.111/b28313/basicmv.htm#i1006734

You can create a nested materialized view on materialized views, but all parent and base materialized views must contain joins or aggregates. If the defining queries for a materialized view do not contain joins or aggregates, it cannot be nested. All the underlying objects (materialized views or tables) on which the materialized view is defined must have a materialized view log. All the underlying objects are treated as if they were tables. In addition, you can use all the existing options for materialized views.

3

可以使TEST4物化視圖刷新快是這樣的:

SQL> create table test1 
    2 (x1 varchar2(1000) 
    3 , constraint test1_pk primary key (x1) 
    4 ) 
    5/

Table created. 

SQL> create materialized view log on test1 with rowid 
    2/

Materialized view log created. 

SQL> create table test2 
    2 (x2 varchar2(1000) 
    3 , constraint test2_pk primary key (x2) 
    4 ) 
    5/

Table created. 

SQL> create materialized view log on test2 with rowid 
    2/

Materialized view log created. 

SQL> create materialized view test4 
    2 refresh fast on commit 
    3 as 
    4 select t1.rowid as rid1 
    5   , t2.rowid as rid2 
    6   , t1.x1 u1 
    7   , t2.x2 
    8  from test1 t1 
    9   , test2 t2 
10  where t1.x1 = t2.x2 
11/

Materialized view created. 

SQL> insert into test1 values ('hello') 
    2/

1 row created. 

SQL> insert into test2 values ('hello') 
    2/

1 row created. 

SQL> commit 
    2/

Commit complete. 

SQL> select * from test4 
    2/

RID1    RID2 
------------------ ------------------ 
U1 
--------------------------------------------- 
X2 
--------------------------------------------- 
AAATU5AAEAAAssfAAA AAATU8AAEAAAssvAAA 
hello 
hello 


1 row selected. 

您的情況不適用,因爲對於嵌套的MV工作,底層MV不能是基本的MV。這聽起來很奇怪,但你需要像test3那樣的技巧來使它工作。另外,要使聯合MV工作,需要使用ROWID創建基礎表的物化視圖日誌。

您可能想看看我寫的關於快速刷新物化視圖錯誤的一系列博文。他們描述了幾乎所有的限制:

Basic MV's
Join MV's
Aggregate MV's
Union all MV's
Nested MV's
MV_CAPABILITIES_TABLE
Summary

問候,
羅布。


添加:29-09-2011

下面是使用UNION ALL招嵌套MV上test2的還有一個例子:

SQL> create table test1 
    2 (x1 varchar2(1000) 
    3 , constraint test1_pk primary key (x1) 
    4 ) 
    5/

Table created. 

SQL> create materialized view log on test1 with rowid 
    2/

Materialized view log created. 

SQL> create table test2 
    2 (x2 varchar2(1000) 
    3 , constraint test2_pk primary key (x2) 
    4 ) 
    5/

Table created. 

SQL> create materialized view log on test2 with rowid 
    2/

Materialized view log created. 

SQL> create materialized view test2_mv 
    2 refresh fast on commit 
    3 as 
    4 select rowid rid 
    5  , x2 
    6  , 'A' umarker 
    7 from test2 
    8 union all 
    9 select rowid 
10  , x2 
11  , 'B' 
12 from test2 
13 where 1=0 
14/

Materialized view created. 

SQL> alter table test2_mv add constraint test2_mv_pk primary key(x2) 
    2/

Table altered. 

SQL> create materialized view log on test2_mv with rowid 
    2/

Materialized view log created. 

SQL> create materialized view test3 
    2 refresh fast on commit 
    3 as 
    4 select rowid rid 
    5  , x1 
    6  , 'A' umarker 
    7 from test1 
    8 union all 
    9 select rowid 
10  , x1 
11  , 'B' 
12 from test1 
13 where 0 = 1 
14/

Materialized view created. 

SQL> alter table test3 add constraint test3_pk primary key (x1) 
    2/

Table altered. 

SQL> create materialized view log on test3 with rowid 
    2/

Materialized view log created. 

SQL> create materialized view test4 
    2 refresh fast on commit 
    3 as 
    4 select t1.rowid as rid1 
    5   , t2.rowid as rid2 
    6   , t1.x1 u1 
    7   , t2.x2 
    8  from test3 t1 
    9   , test2_mv t2 
10  where t1.x1 = t2.x2 
11/

Materialized view created. 

SQL> insert into test1 values ('hello') 
    2/

1 row created. 

SQL> insert into test2 values ('hello') 
    2/

1 row created. 

SQL> commit 
    2/

Commit complete. 

SQL> select * from test4 
    2/

RID1    RID2 
------------------ ------------------ 
U1 
--------------------------------------------------- 
X2 
--------------------------------------------------- 
AAATXbAAEAAAstdAAA AAATXXAAEAAAstNAAA 
hello 
hello 


1 row selected. 

希望這有助於!

+0

「這聽起來很奇怪,但你需要一個像你用test3做的工作,使其工作」: 你的回答只給出了代碼,以便在兩張表上建立物化視圖,我已經可以做到這一點。你可以給我示例代碼,這個技巧可以讓我在快速刷新視圖的基礎上完成刷新按需視圖? – Clinton

+0

解決方案取決於您的完整刷新視圖的樣子。它是基本的,連接,聚合,聯合還是嵌套MV?在你的例子中,test3 MV是不必要的。 –

+0

或者是你的test3 MV不是爲了讓它成爲一個聯盟所有的MV而不是一個基本的MV,我想知道...... –