物化视图快速刷新-更新基表时插入和删除

时间:2020-09-23 06:47:15

标签: oracle plsql oracle19c

你好,Stackoverflowers,

TLDR:刷新期间是否使用app_name = 'contacts' urlpatterns = [ path('list/', contacts_list, name='contact_list'), path('', contact_us, name='contact'), ] UPDATE使用MVIEW?

前一段时间,当我在Oracle中摆弄具体化视图时遇到了一个晦涩的问题。这是我的示例:

  • 2个基本表
  • 两个表的MVIEW日志
  • 两个表的
  • PKs
  • 将MVIEW创建为这些基本表的联接
  • MVIEW的PK

这是示例代码:

DELETE + INSERT

根据-- ========================= DDL section ========================= /* drop tables */ drop table tko_mview_test_tb; drop table tko_mview_test2_tb; /* drop mview */ drop materialized view tko_mview_test_mv; /* create tables */ create table tko_mview_test_tb as select 1111 as id, 'test' as code, 'hello world' as data, sysdate as timestamp from dual union select 2222, 'test2' as code, 'foo bar', sysdate - 1 from dual; create table tko_mview_test2_tb as select 1000 as id, 'test' as fk, 'some string' as data, sysdate as timestamp from dual; /* create table PKs */ alter table tko_mview_test_tb add constraint mview_test_pk primary key (id); alter table tko_mview_test2_tb add constraint mview_test2_pk primary key (id); /* create mview logs */ create materialized view log on tko_mview_test_tb with rowid, (data); create materialized view log on tko_mview_test2_tb with rowid, (data); /* create mview */ create materialized view tko_mview_test_mv refresh fast on commit as select a.code , a.data , b.data as data_b , a.rowid as rowid_a , b.rowid as rowid_b from tko_mview_test_tb a join tko_mview_test2_tb b on b.fk = a.code; /* create mview PK */ alter table tko_mview_test_mv add constraint mview_test3_pk primary key (code); 我的MVIEW能否快速刷新。

在这种特殊情况下(此处不是示例),FVIEW从其他表中引用了MVIEW。因此,我发现,当我对这些基本表之一进行更改并触发MVIEW刷新时,我收到一条错误消息:

dbms_mview.explain_mview

我就像什么鬼?? 。因此,我开始进行挖掘-在该MVIEW上创建了一个触发器。像这样:

ORA-12048: error encountered while refreshing materialized view "ABC"
ORA-02292: integrity constraint (ABC_FK) violated

所以我能够看到发生了什么。根据我的触发器,每次我在基表中执行 UPDATE (不是INSERT或DELETE)时,实际上MVIEW表上都会进行 DELETE和INSERT 操作。

/* create trigger on MVIEW */  
create or replace trigger tko_test_mview_trg
  after insert or update or delete
  on tko_mview_test_mv
  referencing old as o new as n
  for each row
declare
begin
  if updating then
    dbms_output.put_line('update');
  elsif inserting then
    dbms_output.put_line('insert');
  elsif deleting then
    dbms_output.put_line('delete');
  end if;  
end tko_test_mview_trg;
/

输出

update tko_mview_test2_tb
   set data = 'some sting'
 where id = 1000; 
commit;

MVIEW刷新的工作方式是否正确?刷新MVIEW时,MVIEW表上没有更新吗?

关于, 汤姆

1 个答案:

答案 0 :(得分:1)

从oracle 12.1升级到oracle 19.x后,我们看到了相同的行为

新创建的mview的行为似乎相同,即在刷新过程中删除/插入而不是“预期”更新。不确定是不好还是错误.....但是可以“修复”。

应用补丁30781970(不要忘记_fix_control)并重新创建mview .....

参考:错误30781970-MVIEW刷新失败,并且MVIEW上触发了当前ORA-1错误(文档ID 30781970.8)