XQuery中的Marklogic日期比较,有或没有索引

M_b*_*eeb 3 marklogic

我需要使用Marklogic 8按日期(上周,上个月等)过滤文档.该数据库包含130万个XML文档.

文件看起来像这样:

<work datum_gegenereerd="2015-06-10" gegenereerd="2015-06-10T14:28:48" label="gmb-2015-12000">
 ...
Run Code Online (Sandbox Code Playgroud)

我在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>
Run Code Online (Sandbox Code Playgroud)

经过大量的实验,我发现通过从where语句中删除xs:date cast,我可以将性能降低到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>
Run Code Online (Sandbox Code Playgroud)

谁能解释这种行为?


更新:
当我删除属性范围索引时,第二个变体的性能也会下降到3秒以上.并重新创建索引可以提升性能.这让我想知道如何阅读下面的David的声明,没有办法使用普通xquery的自定义索引.(顺便说一下:查询返回1267个XML文档,在一个可能的450000个文档中,根元素在135万个文档的总数据库中工作)
更新2:
我搞砸了0.02秒的性能指标.但它在查询控制台中非常快.在3个版本中,cts-search似乎要快一点.

Dav*_*nis 6

您可能已创建索引,但未使用它.您需要使用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'))
Run Code Online (Sandbox Code Playgroud)

但是:如果你真的只想要URIS,那么元素范围查询将与cts一起使用:uris(像这样的sometihng - 但检查文档)

cts:uris('', (), cts:element-attribute-range-query(xs:QName("work"), xs:QName("datum_gegenereerd"), ">" current-date()-   5*xs:dayTimeDuration('P1D'))
Run Code Online (Sandbox Code Playgroud)

第二个在内存中完成所有操作,只是从URI词典中提取URI,指向日期查询匹配的文档片段.