gpt4 book ai didi

sql - Oracle - 如何使用 FAST REFRESH 和 JOINS 创建物化 View

转载 作者:搜寻专家 更新时间:2023-10-30 20:53:13 25 4
gpt4 key购买 nike

所以我很确定 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: 无法为物化 View 设置 ON COMMIT 刷新属性

我已经在 TPM_PROJECT 和 TPM_PROJECTVERSION 上创建了物化 View 日志。 TPM_PROJECT 的主键为 PROJECTID,TPM_PROJECTVERSION 的复合主键为 (PROJECTID,VERSIONID)。这有什么诀窍?我一直在挖掘 Oracle 手册,但无济于事。谢谢!

最佳答案

首先,来自 Oracle Database Data Warehousing Guide :

Restrictions on Fast Refresh on Materialized Views with Joins Only

...

  • Rowids of all the tables in the FROM list must appear in the SELECT list of the query.

这意味着您的声明需要看起来像这样:

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

另一个需要注意的关键方面是您的物化 View 日志必须创建为 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;

关于sql - Oracle - 如何使用 FAST REFRESH 和 JOINS 创建物化 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com