Oracle:如何创建一个快速刷新的物化视图,从XMLType中提取数据?

发布于 2024-10-30 19:00:14 字数 715 浏览 0 评论 0原文

我有一个包含两列的表 xml_documents:一个 document_id 列(主键)和一个包含一些 XML 数据的 xml 列,这是一个架构-less XMLType。我可以仅使用 document_id 创建一个物化视图:

create materialized view mv refresh fast on commit as 
select document_id
from xml_documents

这工作正常,但不是很有用。正如您所期望的,我希望物化视图从 XML 中提取数据,为此我使用 extractValue()。我正在尝试以下操作:

create materialized view mv refresh fast on commit as 
select document_id, extractValue(xml, '/my/gaga') gaga
from xml_documents

失败并显示:

ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view

我应该如何在提交物化视图上创建快速刷新以从 XML 中提取值?

I have a table xml_documents with two columns: a document_id column (primary key) and an xml column with some XML data, which is a schema-less XMLType. I can create a materialized view with just the document_id with:

create materialized view mv refresh fast on commit as 
select document_id
from xml_documents

This works fine, but isn't very useful. As you might expect, I'd like the materialized view to extract data from the XML, and for this I use extractValue(). I am trying the following:

create materialized view mv refresh fast on commit as 
select document_id, extractValue(xml, '/my/gaga') gaga
from xml_documents

This fails with:

ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view

How should I go about to create a fast refresh on commit materialized view that extract values from XML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2024-11-06 19:00:14

您的 XMLType(可能)存储为 CLOB。使用如下查询查找隐藏列:

select * from user_tab_cols where table_name = 'XML_DOCUMENTS';

然后创建一个函数将 CLOB 转换为 XMLType,并提取值。请注意,“确定性”关键字是必要的,尽管
我不知道为什么。在 SQL 和 PL/SQL 之间来回传递数据会很慢,但如果您使用物化视图,事情可能会很慢
已经很慢了。

create or replace function extract_from_clob(p_xml in clob) return varchar2 deterministic
  is
begin
    return XMLType(p_xml).extract('/my/gaga/text()').getStringVal();
end;
/

然后删除并创建物化视图,并将系统列传递到函数中:

create materialized view mv refresh fast on commit as 
select document_id, extract_from_clob(SYS_NC00003$) gaga
from xml_documents;

我不确定是否使用系统生成的隐藏列。它有效,但似乎不是一个真正的好主意。至少它会成功
在不同的系统上创建对象很困难 - 您每次都需要找到新的列名称。

当 LOB 工作正常时 XMLType 却不起作用,这似乎很奇怪。我找不到任何关于此的文档;我不确定这是否是一个错误,一个未实现的功能,或者是否有一些神奇的设置可以让它工作。如果没有其他人可以提供更好的答案,那么在使用上述方法之前可能需要先咨询 Oracle 支持人员。

Your XMLType is (probably) stored as a CLOB. Find the hidden column with a query like this:

select * from user_tab_cols where table_name = 'XML_DOCUMENTS';

Then create a function to convert a CLOB into an XMLType, and extract the value. Note that the "deterministic" keyword is necessary, although
I'm not sure why. Passing data back and forth between SQL and PL/SQL will be slow, but if you're using a materialized view things are probably
already slow.

create or replace function extract_from_clob(p_xml in clob) return varchar2 deterministic
  is
begin
    return XMLType(p_xml).extract('/my/gaga/text()').getStringVal();
end;
/

Then drop and create your materialized view with the system column passed into the function:

create materialized view mv refresh fast on commit as 
select document_id, extract_from_clob(SYS_NC00003$) gaga
from xml_documents;

I'm unsure about using a system-generated hidden column. It works, but doesn't seem like a really good idea. At the very least it will make it
difficult to create the object on different systems - you'll need to find the new column name each time.

It seems weird that XMLTypes don't work when LOBs work fine. I can't find any documentation about this; I'm not sure if it's a bug, an unimplemented feature, or if there's some magic setting that will make it work. If no one else can provide a better answer, it might be worth checking with Oracle support before you use the above approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文