【问题标题】:Oracle materialized view using with "not exists"Oracle物化视图使用“不存在”
【发布时间】:2013-06-15 00:44:46
【问题描述】:

我知道 Oracle 物化视图不能使用“不存在”子句快速刷新。 有解决办法吗。我尝试使用左外连接和 (+) 但这两个选项似乎也不起作用。任何帮助表示赞赏

create materialized view mv_myview refresh fast as 
select a.* 
from tableA a 
where 
    not exists (select * from tableB b where a.my_id = b.my_id); 

【问题讨论】:

  • 请显示视图的脚本
  • 创建物化视图 mv_myview 刷新速度快为 select a.* from tableA a where not exists (select * from tableB b where a.my_id = b.my_id);

标签: oracle materialized-views


【解决方案1】:

启用快速刷新很棘手,有许多奇怪的限制和无用的错误消息。在这种情况下,您需要创建一个物化视图日志WITH ROWID,使用(+) 连接语法,并为每个表添加一个ROWID

create table tablea(my_id number primary key, a number);
create table tableb(my_id number primary key, b number);

create materialized view log on tablea with rowid;
create materialized view log on tableb with rowid;

create materialized view mv_myview refresh fast on commit as 
select a.my_id, a.a, b.b, a.rowid a_rowid, b.rowid b_rowid
from tableA a, tableB b
where a.my_id = b.my_id(+)
    and b.My_id IS NULL;

insert into tablea values(1, 1);
commit;

select * from mv_myview;

MY_ID  A  B  A_ROWID             B_ROWID
-----  -  -  -------             -------
1      1     AAAUH3AAEAAC+t0AAA  

【讨论】:

  • 这几乎可以在我提交时挂起:-(
  • 基本上我看不到物化视图可以通过快速刷新支持“不在”子句的方式。是不是Oracle没有办法用“not in”快速刷新?
  • 不幸的是,这里有很多可能出错的地方。更新物化视图的递归 SQL 可能存在问题。提交运行时查看select * from V$SQL where users_executing > 0,然后查看计划。希望只是收集统计数据会有所帮助。此外,您可能需要解释物化视图:begin dbms_mview.explain_mview('MV_MYVIEW'); end;。如果这不起作用,您可能需要创建 MV_CAPABILITIES_TABLE,它在脚本 utlxmv.sql 中可用。
  • (我会在该文件中发布create table 脚本,但是当我尝试这样做时,SO 给了我一个错误。)您使用的是什么版本?它适用于我 11.2.0.3。您可能需要发布完整的 DDL 和一些脚本来创建示例数据,以便我们重现问题。抱歉有这些问题。根据我的经验,物化视图总是很难使用。
【解决方案2】:

在 oracle 11 下执行查询,出现以下错误:

使用 LEFT JOIN,我遇到了同样的问题:

create materialized view mv_myview refresh fast as 
select a.* 
from tableA a LEFT JOIN tableB b ON a.my_id = b.my_id
where 
    b.id IS NULL; 

使用 NOT IN 时同样的问题...

create materialized view mv_myview refresh fast as 
select a.* 
from tableA a 
where 
    a.my_id not in (select b.my_id from tableB b); 

急救信息很清楚:

ORA-12015: 无法从 复杂查询原因:既不是 ROWID,也不是主键约束 支持复杂查询。行动:重新发出命令 REFRESH FORCE 或 REFRESH COMPLETE 选项或创建一个简单的 物化视图。

这个问题似乎不可能。您必须更改视图类型。

【讨论】:

    【解决方案3】:

    我想不出一个完整的解决方法。如果不存在导致的反连接由于某种原因效率低下,那么您可以基于优化创建一个快速刷新 MV:

    select my_id, count(*)
    from   tab
    group by my_id
    

    反连接通常非常有效。您不只是缺少索引吗?

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 2012-07-17
      • 2010-11-21
      • 2011-11-01
      • 2010-10-14
      相关资源
      最近更新 更多