2

我在我的 Xquery 中使用了一种特定的方法,如下所示

declare function ctrlv($node, $node-id as xs:string) 
{
    <z:b> 
    {
        (attribute {"xml:id"}{$node/@xml:id})
      }</z:b>
};

这个特殊的函数使用 diff $node 循环了 11,000 次。仅给定的方法就占用了整个响应时间的 50-60%(大约 8 秒)。当我进行性能分析时,它显示 $node/@xml:id 花费了最长时间。在这种情况下如何提高性能?请帮忙

4

4 回答 4

2

我不太清楚为什么要构造一个与从中获取的名称相同的新属性$node并插入@id. 由于使用元素的构造应该有一些开销,我猜下面的速度更快(并且应该提供相同的输出):

declare function ctrlv($node, $node-id as xs:string) 
{
    <z:b> 
      {$node/@xml:id}
    </z:b>
};

此外,$node-id从不在您的函数中使用,因此您可能希望将其作为参数删除。

于 2013-09-13T09:46:32.763 回答
1

There are incremental steps to improving speed of these things. Creation of XML elements is somewhat expensive (although .3 ms is not that bad ... but if you do 10k of those it does add up)

Here are some steps when you identify the most frequenly used expressions. They are suggestions of what to do after you identify performance issues, not before, because most often the performance impact is minimal and code readability and maintainability is more important. But once you identify a hot spot consider these:

limit function calls - something this simple can be done inline without a function. Limit binding to variables Limit for loops Limit dynamic construction of elements and attributes

Of course you cant do all of these but you can decide where the pain is most and apply the concepts there. For example the above call could instead be inlined as

<z:b xml:id="{$node/@xml:id}"/>

Try placing this inline where you tried the function and see what the results are.

Also note that the profiler can sometimes give misleading information. Many expressions are lazily evaluated and tend to attribute their time to where they are used instead of where they are declared.

于 2013-09-13T13:26:16.100 回答
0

你的电脑是不是特别慢?探查器告诉我这在 250 毫秒内运行。

declare function local:do($node) 
{
  element b { $node/@xml:id }
};

distinct-values(
  (1 to 10 * 1000) ! local:do(<test xml:id="a123"/>))

要尝试的另一件事是 XSLT 实现。您仍然需要计算$node/@xml:id11,000 次,但达到该表达式的速度要快一些。

于 2013-09-13T15:19:22.433 回答
-2

如果您正在使用相同的 uri 将修改后的文档写回数据库,请尝试使用 xdmp:node-replace() 函数来修改属性值,而不是在内存中构建新的文档树并将其写回数据库.

如果不是,循环中会发生什么?您是在每次迭代中构造一个新文档,还是迭代器使用类型开关在树上递归,构造新属性及其祖先元素但复制所有其他节点?

于 2013-09-13T14:05:35.140 回答