【问题标题】:How to use dbms_xmldom in Oracle PLSQL in order to get a good function performance?如何在 Oracle PLSQL 中使用 dbms_xmldom 以获得良好的功能性能?
【发布时间】:2018-04-21 17:39:38
【问题描述】:

我使用的是 Oracle 版本 12 第 1 版

我一直在尝试编写一个函数来计算存储为 XML 的对象之间的某种距离。

为此,我做了以下...

首先,注册 XML 模式。

BEGIN
-- Register the schema
DBMS_XMLSCHEMA.registerSchema('http://www.example.com/fvInteger.xsd',
'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="FeatureVector">
<xs:complexType>
<xs:sequence>
<xs:element name="feature" type="xs:integer" minOccurs="5" maxOccurs="999"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>',
   TRUE, TRUE, FALSE);
END;
/

然后创建表

CREATE TABLE cophirfvXML_int (
   id    NUMBER,
   complex_obj  XMLTYPE)
   XMLTYPE complex_obj STORE AS OBJECT RELATIONAL
      XMLSCHEMA "http://www.example.com/fvInteger.xsd"
      ELEMENT "FeatureVector";

在上表中插入以下数据

ID=1
<FeatureVector><feature>85</feature><feature>-41</feature><feature>29</feature><feature>26</feature><feature>-29</feature><feature>1</feature><feature>-29</feature><feature>-8</feature><feature>15</feature><feature>6</feature><feature>-17</feature><feature>6</feature><feature>-27</feature><feature>8</feature><feature>-12</feature><feature>5</feature></FeatureVector>

ID=2
<FeatureVector><feature>98</feature><feature>77</feature><feature>-127</feature><feature>27</feature><feature>-30</feature><feature>-13</feature><feature>-1</feature><feature>14</feature><feature>-31</feature><feature>-56</feature><feature>-10</feature><feature>6</feature><feature>-10</feature><feature>-12</feature><feature>5</feature><feature>19</feature></

FeatureVector>

... and so on (I am working with 200.000 objects).

最后是函数>>

create or replace FUNCTION myDistance(
  innXML XMLType,
  outXML XMLType
) RETURN number
IS
  total NUMBER := 0;
  docInn xmldom.DOMDocument;
  docOut xmldom.DOMDocument;
  nInn xmldom.DOMNode;
  nOut xmldom.DOMNode;
  nlInn xmldom.DOMNodeList;
  nlOut xmldom.DOMNodeList;
  len number;
BEGIN
--Converte os atributos xmltype para DOMDocuments.
docInn := dbms_xmldom.newDOMDocument(innXML);
docOut := dbms_xmldom.newDOMDocument(outXML);

nlInn := xmldom.getElementsByTagName(docInn, '*');
nlOut := xmldom.getElementsByTagName(docOut, '*');
len := xmldom.getLength(nlInn);

for i in 1..len-1 loop
    nInn := xmldom.item(nlInn, i);
        nOut := xmldom.item(nlOut, i);
    total := total + ABS(xmldom.getNodeValue(DBMS_XMLDOM.getFirstChild(nInn)) - xmldom.getNodeValue(DBMS_XMLDOM.getFirstChild(nOut)));
end loop;

RETURN total;
END;
/

该函数的性能很糟糕。它使用了太多的内存并且比预期的要慢得多(主要是因为使用了对象关系存储)。

我什至得到了错误:

ORA-00039: erro durante ac?o periodica ORA-04036: Memoria PGA usada pela instancia 超过 PGA_AGGREGATE_LIMIT ORA-06512: em “XDB.DBMS_XMLDOM”,第 5027 行 ORA-06512:em “XDB.DBMS_XMLDOM”,行 5052 ORA-06512: em "HIGIIA.XML_MANHATTAN_DISTANCE",第 19 行

另外,我在下面使用查询尝试了这种不同的解决方案,但性能也不好。

SELECT SUM( ABS(oFV.feature - iFV.feature) )
  INTO   total
  FROM   XMLTABLE(
           '//FeatureVector/feature'
           PASSING outXML
           COLUMNS rn       FOR ORDINALITY,
                   feature  NUMBER  PATH '.'
         ) oFV
         INNER JOIN
         XMLTABLE(
           '//FeatureVector/feature'
           PASSING innXML
           COLUMNS rn       FOR ORDINALITY,
                   feature  NUMBER  PATH '.'
         ) ifv
         ON ( oFV.rn = iFV.rn );

我可以做些什么来提高它的性能?

我确实需要提高性能,而不是解决 ORA-00039 错误 增加 PGA 聚合限制。

希望有人可以提供帮助!提前致谢!!

【问题讨论】:

    标签: xml oracle plsql domdocument oracle12c


    【解决方案1】:

    几件事 - 我猜你的内存不足是由于没有调用 DBMS_XMLDOM.freeDocument(docXXX);

    但至于性能问题 - 如果没有及时说明,很难说代码是否存在问题,或者这是否是与解析相关 xmltype 字段值相关的基本上预期的开销。我的直接印象是,预先计算并存储“距离”值(当插入/更新/删除 xml 时)。这样您就可以通过直接 sql 查询数据,而无需读取端的所有解析开销。如果要保持 xml 架构干净,可以将计算值存储在 xml 或关系表中。

    【讨论】:

    • 感谢您的回答!它可以被索引(这意味着一些距离将被预先计算并存储在那里)但它使用大量内存,有时它不是 interisting。
    • 它解决了内存问题并提高了性能(尽管它仍然不是那么好)...
    猜你喜欢
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2020-11-07
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多