【发布时间】: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