1

我正在编写一些 Xquery 代码(使用 SAXON)来针对大型 XML 文件执行简单的 XQuery 文件。

XML 文件(位于 this.referenceDataPath)有 300 万个“行”节点,格式如下:

<row>
<ISRC_NUMBER>1234567890</ISRC_NUMBER>
</row>
<row>
<ISRC_NUMBER>1234567891</ISRC_NUMBER>
</row>
<row>
<ISRC_NUMBER>1234567892</ISRC_NUMBER>
</row>

ETC...

XQuery 文档(位于 this.xqueryPath)是:

declare variable $isrc as xs:string external;
declare variable $refDocument external;
let $isrcNode:=$refDocument//row[ISRC_NUMBER=$isrc]
return count($isrcNode)

Java代码是:

private XQItem referenceDataItem;
private XQPreparedExpression xPrepExec;
private XQConnection conn;

//set connection string and xquery file
this.conn = new SaxonXQDataSource().getConnection();
InputStream queryFromFile = new FileInputStream(this.xqueryPath);

//Set the prepared expression 
InputStream is  = new FileInputStream(this.referenceDataPath);
this.referenceDataItem = conn.createItemFromDocument(is, null, null);
this.xPrepExec = conn.prepareExpression(queryFromFile);
xPrepExec.bindItem(new QName("refDocument"), this.referenceDataItem);   

//the code below is in a seperate method and called multiple times
public int getCount(String searchVal){

    xPrepExec.bindString(new QName("isrc"), searchVal, conn.createAtomicType   (XQItemType.XQBASETYPE_STRING));

    XQSequence resultsFromFile = xPrepExec.executeQuery();
    int count = Integer.parseInt(resultsFromFile.getSequenceAsString(new Properties()));
    return count;

}

方法 getCount 被连续调用多次(例如 1000000 次)以验证 XML 文件中是否存在许多值。

Xquery 查询的当前速度对于每次调用 getCount 大约需要 500 毫秒,考虑到 XML 文档在内存中并且查询是准备好的,这似乎非常慢。

我使用 XQuery 的原因是作为未来工作的概念证明,其中 XML 文件将具有更复杂的布局。

我在具有 8GB RAM 的 i7 上运行代码,因此内存不是问题——我还增加了程序的分配堆大小。

关于如何提高此代码的速度的任何建议?

谢谢!

4

2 回答 2

1

Zorba 具有解析和查询大型 XML 文档的功能。有关它的一些文档,请访问http://www.zorba-xquery.com/html/entry/2012/05/31/XML_Streaming

例如,在下面的代码片段中,我们通过 HTTP 解析一个 700MB 的文档,整个过程以流式方式从上到下进行:

import module namespace http = "http://expath.org/ns/http-client";
import module namespace p = "http://www.zorba-xquery.com/modules/xml";
import schema namespace opt = "http://www.zorba-xquery.com/modules/xml-options";

let $raw-data as xs:string := http:send-request(<http:request href="http://cf.zorba-xquery.com.s3.amazonaws.com/forecasts.xml" method="GET" override-media-type="text/plain" />)[2]
let $data := p:parse($raw-data, <opt:options><opt:parse-external-parsed-entity opt:skip-root-nodes="1"/></opt:options>)
return
    subsequence($data, 1, 2) 

您可以在http://www.zorba-xquery.com/html/demo#CGPfEyXKvDwDfgzek/VTOIAIrJ8=现场试用此示例

于 2012-06-06T19:51:16.327 回答
1

对于如何提高速度的问题,最明显的答案是尝试 Saxon-EE,它具有更强大的优化器,并且还使用字节码生成。我没有尝试过,但我认为 Saxon-EE 会检测到该查询将受益于构建索引,并且每次出现查询都会重复使用相同的索引。

我要提出的另一个建议是声明变量 $refDocument 的类型 - 类型信息有助于优化器做出更明智的决策。例如,如果优化器知道 $refDocument 是单个节点,那么它知道 $refDocument//X 将自动按文档顺序排列,而无需任何排序操作。

用“eq”替换“=”运算符也值得一试。

于 2012-06-09T23:51:25.980 回答