【问题标题】:How to minimize the use of FOR loop in XQuery?如何在 XQuery 中尽量减少 FOR 循环的使用?
【发布时间】:2018-07-27 09:36:19
【问题描述】:

我有如下代码,我想知道是否可以在不使用 FOR 循环的情况下获得相同的结果-

let $doc :=  cts:search(fn:doc(), 
                cts:element-value-query(......))

let $column-values := ("one,two,three....") (: Can be n number of values :)

let $tokenized-values := fn:tokenize($column-values, ",")

let $result := for $i in $doc
               let $temp := for $j in $tokenized-values
                              return fn:concat("$i//*:",$j)

                    return <root>{xdmp:value($temp)}</root>

return <result>{$result}</result>

预期结果如下-

<result>
  <root>
    <one>abc</one>
    <two>456</two>
    <three>675</three>
  </root>
  <root>
    <one>dfd</one>
    <two>235</two>
    <three>765</three>
  </root>
</result>

我得到了结果,但是如果我想尽量减少 FOR 循环的使用,我怎样才能得到相同的结果。

有什么建议吗?

【问题讨论】:

  • 你只有两个for .. in表达式,为什么要消除它们? Marklogic 是否支持! 运算符?这肯定是不使用for 的一种方法,所以$tokenized-values!(concat("$i//*:",.) 会重写你的第二个for $j。但你最好先解释一下为什么不想使用for 表达式。
  • 我的第一个“for”将遍历所有文档,无论我从搜索查询中获得什么,它都会降低我的性能。

标签: xquery marklogic


【解决方案1】:

为了提高性能,您可以在要提取的所有列上放置一个范围索引,并使用 cts:element-value-tuples 代替 cts:search 。这将只提取您想要的元素,而不是整个文档。

对于第二个 for 循环的替代语法,您可以使用以下语法:

for $j in $tokenized-values
                          return fn:concat("$i//*:",$j)

$tokenized-values ! fn:concat("$i//*:", .)

虽然在性能方面大致相同。

【讨论】:

  • Rob- 谢谢 :) 我也会尝试使用 cts:element-value-tuples。
  • 其实用XQ31你可以写$tokenized-values ! ("$i//*:" || .)
  • MarkLogic 的1.0-ml XQuery 语法模式支持!||,以及其他一些较新的XQuery 运算符和功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 2022-01-25
相关资源
最近更新 更多