it-swarm.cn

Oracle-如何使用FAST REFRESH和JOINS创建实例化视图

因此,我非常确定Oracle支持这一点,所以我不知道我在做什么错。此代码有效:

CREATE MATERIALIZED VIEW MV_Test
  NOLOGGING
  CACHE
  BUILD IMMEDIATE 
  REFRESH FAST ON COMMIT 
  AS
    SELECT V.* FROM TPM_PROJECTVERSION V;

如果我添加一个JOIN,它将中断:

CREATE MATERIALIZED VIEW MV_Test
  NOLOGGING
  CACHE
  BUILD IMMEDIATE 
  REFRESH FAST ON COMMIT 
  AS
    SELECT V.*, P.* FROM TPM_PROJECTVERSION V
    INNER JOIN TPM_PROJECT P ON P.PROJECTID = V.PROJECTID

现在我得到了错误:

ORA-12054:无法为实例化视图设置ON COMMIT刷新属性

我已经在TPM_PROJECT和TPM_PROJECTVERSION上创建了实例化视图日志。 TPM_PROJECT具有PROJECTID主键,TPM_PROJECTVERSION具有复合主键(PROJECTID,VERSIONID)。这有什么窍门?我一直在浏览Oracle手册,但都没有用。谢谢!

35
Mike Christensen

首先,从 Oracle数据库数据仓库指南

仅具有联接的物化视图的快速刷新限制

...

  • FROM列表中所有表的Rowid必须出现在查询的SELECT列表中。

这意味着您的陈述将需要如下所示:

CREATE MATERIALIZED VIEW MV_Test
  NOLOGGING
  CACHE
  BUILD IMMEDIATE 
  REFRESH FAST ON COMMIT 
  AS
    SELECT V.*, P.*, V.ROWID as V_ROWID, P.ROWID as P_ROWID 
    FROM TPM_PROJECTVERSION V,
         TPM_PROJECT P 
    WHERE P.PROJECTID = V.PROJECTID

要注意的另一个关键方面是,您的物化视图日志必须创建为with rowid

以下是功能测试方案:

CREATE TABLE foo(foo NUMBER, CONSTRAINT foo_pk PRIMARY KEY(foo));

CREATE MATERIALIZED VIEW LOG ON foo WITH ROWID;

CREATE TABLE bar(foo NUMBER, bar NUMBER, CONSTRAINT bar_pk PRIMARY KEY(foo, bar));

CREATE MATERIALIZED VIEW LOG ON bar WITH ROWID;

CREATE MATERIALIZED VIEW foo_bar
  NOLOGGING
  CACHE
  BUILD IMMEDIATE
  REFRESH FAST ON COMMIT  AS SELECT foo.foo, 
                                    bar.bar, 
                                    foo.ROWID AS foo_rowid, 
                                    bar.ROWID AS bar_rowid 
                               FROM foo, bar
                              WHERE foo.foo = bar.foo;
45
Allan

您是否尝试了没有ANSI连接的尝试?

CREATE MATERIALIZED VIEW MV_Test
  NOLOGGING
  CACHE
  BUILD IMMEDIATE 
  REFRESH FAST ON COMMIT 
  AS
SELECT V.*, P.* FROM TPM_PROJECTVERSION V,TPM_PROJECT P 
WHERE  P.PROJECTID = V.PROJECTID
5
steve godfrey

如果未为查询所引用的主表创建实例化视图日志,则将在REFRESH_FAST上收到错误。如果任何人都不熟悉或第一次使用实例化视图,更好的方法是使用Oracle sqldeveloper并以图形方式放置选项,并且错误也可以提供更好的理解。

3
Paul

快速刷新的关键检查包括以下内容:

1) An Oracle materialized view log must be present for each base table.
2) The RowIDs of all the base tables must appear in the SELECT list of the MVIEW query definition.
3) If there are outer joins, unique constraints must be placed on the join columns of the inner table.

3号很容易错过,值得在这里强调

1
pahariayogi