我需要使用 Marklogic 8 按日期(上周、上个月等)过滤文档。数据库包含 130 万个 XML 文档。
文件看起来像这样:
<work datum_gegenereerd="2015-06-10" gegenereerd="2015-06-10T14:28:48" label="gmb-2015-12000">
...
我在 work/@datum_gegenereerd(标量类型日期)上创建了一个范围元素属性索引。
以下查询有效但速度很慢(3 秒):
xquery version "1.0-ml";
for $a in //work
where xs:date($a/@datum_gegenereerd) > current-date()- 5*xs:dayTimeDuration('P1D')
return
<hit>{base-uri($a)}</hit>
经过大量试验后,我发现通过从 where 语句中删除 xs:date 转换,我可以将性能降低到 0.02 秒。
xquery version "1.0-ml";
for $a in //work
where $a/@datum_gegenereerd > current-date()- 5*xs:dayTimeDuration('P1D')
return
<hit>{base-uri($a)}</hit>
谁能解释这种行为?
更新:
当我删除属性范围索引时,第二个变体的性能也下降到 3+ 秒。重新创建索引可以恢复性能。这让我想知道如何阅读下面大卫的声明,即无法使用普通 xquery 中的自定义索引。
(顺便说一句:查询返回 1267 个 XML 文档,在 135 万个文档的总数据库中,可能有 450000 个具有根元素的文档)
更新 2:
我弄乱了 0.02 秒的性能指标。但它在查询控制台中非常快。在这 3 个版本中,cts-search 似乎要快一点。
最佳答案
您可能已经创建了一个索引,但您没有使用它。您需要使用 element-attribute-range-query查找日期在相关范围内的所有片段。
类似
cts:search(doc(), cts:element-attribute-range-query(xs:QName("work"), xs:QName("datum_gegenereerd"), ">" current-date()- 5*xs:dayTimeDuration('P1D'))
但是:如果您真的只想要 URIS,那么元素范围查询将与 cts:uris 一起使用(sometihng 像这样 - 但检查文档)
cts:uris('', (), cts:element-attribute-range-query(xs:QName("work"), xs:QName("datum_gegenereerd"), ">" current-date()- 5*xs:dayTimeDuration('P1D'))
第二个在内存中执行所有操作,只是从 URI 词典中提取 URI,这些 URI 指向日期查询匹配的文档片段。
关于XQuery 中带或不带索引的 Marklogic 日期比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34924497/