物化视图同步问题
2011-12-20 15:33
253 查看
当物化视图或物化视图的基表(reprebuilt table)上有唯一索引或约束时刷新容易出错!
ORA-12008: error in materialized view refresh path
ORA-00001: unique constraint (TABLE_NAME) violated
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 794
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 851
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 832
ORA-06512: at line 1
下面实例说明及解决方案!
SQL> create unique index uk_test_01 on test (f1, f2);
Index created
SQL> insert into test values(1,1) ;
1 row inserted
SQL> insert into test values(1,2) ;
1 row inserted
SQL> commit;
Commit complete
SQL> create materialized view log on test
2 with sequence, rowid including new values ;
Materialized view log created
SQL> create materialized view mv_test
2 refresh fast on DEMAND
3 with ROWID
4 AS
5 select * from test ;
Materialized view created
SQL> create unique index uk_mv_test on mv_test(f1,f2) ;
Index created
SQL> update test
2 SET
3 f2 = decode(f2, 1, 2, 2, 1)
4 WHERE
5 f1 in (1 , 2);
2 rows updated
SQL> commit;
Commit complete
SQL> exec dbms_mview.refresh('mv_test' , 'f') ;
begin dbms_mview.refresh('mv_test' , 'f'); end;
ORA-12008: error in materialized view refresh path
ORA-00001: unique constraint (UK_MV_TEST) violated
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 794
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 851
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 832
ORA-06512: at line 1
SQL> drop index uk_mv_test;
Index dropped
SQL> alter table mv_test add constraint uk_mv_test unique (f1,f2)deferrable;
Table altered
SQL> exec dbms_mview.refresh('mv_test' , 'f') ;
PL/SQL procedure successfully completed
出错的原因是物化视图刷新是根据物化视图日志单条更新的,不是以一组一组的更新的,所以会出错,加deferrable可以避免这个错误,下面引用一段E文做参考:
Note particularly thedeferrable– when we do our fast refresh there is a moment where we have to have a duplicate row in the table – either we change the 1 to a 2 first, or we change the 2 to a 1 first – the refresh updates one row at a time, it doesn’t do an
array update. By making the constraint deferrable we get a non-unique index protecting the constraint, and Oracle only re-checks the validity of the constraint on thecommit, not for each update statement. I believe there is something in the data warehouse
manuals about making unique (and primary key) constraints onmaterialized viewsdeferrablefor exactly this reason.
ORA-12008: error in materialized view refresh path
ORA-00001: unique constraint (TABLE_NAME) violated
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 794
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 851
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 832
ORA-06512: at line 1
下面实例说明及解决方案!
SQL> create unique index uk_test_01 on test (f1, f2);
Index created
SQL> insert into test values(1,1) ;
1 row inserted
SQL> insert into test values(1,2) ;
1 row inserted
SQL> commit;
Commit complete
SQL> create materialized view log on test
2 with sequence, rowid including new values ;
Materialized view log created
SQL> create materialized view mv_test
2 refresh fast on DEMAND
3 with ROWID
4 AS
5 select * from test ;
Materialized view created
SQL> create unique index uk_mv_test on mv_test(f1,f2) ;
Index created
SQL> update test
2 SET
3 f2 = decode(f2, 1, 2, 2, 1)
4 WHERE
5 f1 in (1 , 2);
2 rows updated
SQL> commit;
Commit complete
SQL> exec dbms_mview.refresh('mv_test' , 'f') ;
begin dbms_mview.refresh('mv_test' , 'f'); end;
ORA-12008: error in materialized view refresh path
ORA-00001: unique constraint (UK_MV_TEST) violated
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 794
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 851
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 832
ORA-06512: at line 1
SQL> drop index uk_mv_test;
Index dropped
SQL> alter table mv_test add constraint uk_mv_test unique (f1,f2)deferrable;
Table altered
SQL> exec dbms_mview.refresh('mv_test' , 'f') ;
PL/SQL procedure successfully completed
出错的原因是物化视图刷新是根据物化视图日志单条更新的,不是以一组一组的更新的,所以会出错,加deferrable可以避免这个错误,下面引用一段E文做参考:
Note particularly thedeferrable– when we do our fast refresh there is a moment where we have to have a duplicate row in the table – either we change the 1 to a 2 first, or we change the 2 to a 1 first – the refresh updates one row at a time, it doesn’t do an
array update. By making the constraint deferrable we get a non-unique index protecting the constraint, and Oracle only re-checks the validity of the constraint on thecommit, not for each update statement. I believe there is something in the data warehouse
manuals about making unique (and primary key) constraints onmaterialized viewsdeferrablefor exactly this reason.
相关文章推荐
- ORACLE 10.2.0.4 物化视图 死锁问题处理手记
- Oracle物化视图,物化视图日志,增量刷新同步远程数据库
- Oracle 11g 通过创建物化视图实现不同数据库间的表数据同步
- 通过物化视图方式同步两服务器中数据表的两种不同方式的比较
- Oracle 使用物化视图实现表数据同步
- 物化视图两个库中表同步
- 用物化视图同步两边数据库
- 物化视图实践(1)----实现远程数据同步
- Oracle 10g通过创建物化视图实现不同数据库间表级别的数据同步
- 不能删除物化视图的问题-由于刷新的物化视图有锁
- 物化视图实践(1)----实现远程数据同步
- 数据库(oracle)数据同步之物化视图(实战入门)
- ListBox设置数据源属性后,数据源和视图的同步更新问题
- Oracle 物化视图创建以及常见问题
- 终于解决了物化视图复制的问题
- Oracle 手动刷新物化视图的问题
- Oracle 11g 通过创建物化视图实现不同数据库间的表数据同步
- 使用物化视图的方式进行表级数据同步示例
- 物化视图的创建写法与刷新问题【转】
- 物化视图快速刷新的一个问题