If an xpath expression has a predicate with value, then element range index 
will be used, if available.
For example, in the following two cases a range index on xs:QName("title") will 
be used:
 a.  /bookstore/book[title="XQuery Programming"]
 b. /bookstore/book[title="XQuery Programming"]/author

However, if there is no value in the predicate then the range index will not be 
used:
 c. .  /bookstore/book[title]

The same logic applies to where clause. If there is a value in the where clause 
and a corresponding element range index, it will be used.
It is different in order by, where an index gets used even though there is no 
comparison predicate and value.

In 6.0.1-1, XPath based index can be defined. There are many situations when 
MarkLogic server attempts to find matching path range index. Again in order to 
use range index, there must be a comparison predicate in the query.

Thanks,

Gajanan

-----Original Message-----
From: general-boun...@developer.marklogic.com 
[mailto:general-boun...@developer.marklogic.com] On Behalf Of Michael Blakeley
Sent: Monday, October 22, 2012 9:47 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Range Index in Where Clause

I would rephrase this as "does the evaluator ever use indexes for where 
clauses?" If indexes are used at all, I would expect ranges to work too. And 
with 6.0-1.1 the answer seems to be "yes" - but I would still recommend doing 
as much as possible in XPath predicates.

Here's a contrived test. In real life it would be very odd to have a 
dls:created element in the main document, but since the range index comes 
preconfigured it's handy for testing.

declare namespace dls="http://marklogic.com/xdmp/dls";;

xdmp:document-insert(
  'test/1',
  element a {
    element b { 'c' },
    element dls:created { xs:dateTime(xs:date('2012-12-01')) } }), 
xdmp:document-insert(
  'test/2',
  element a {
    element b { 'c' },
    element dls:created { xs:dateTime(xs:date('2012-12-12')) } }) => ()

Now we have two documents that match the /a[b eq 'c'] portion of the 
expression, but with different dsl:created values. Let's write a test query 
that matches one, but not the other:

declare namespace dls="http://marklogic.com/xdmp/dls";;
xdmp:query-trace(true()),
for $n in /a[ b eq 'c' ]
where $n/dls:created < xs:dateTime(xs:date('2012-12-12'))
return $n

One document should match the dls:created constraint, and the other shouldn't. 
The actual results match expectations, but that could be due to filtering. 
Let's check the trace:

2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Analyzing path for $n: 
fn:collection()/a[b eq "c"]
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Step 1 is searchable: 
fn:collection()
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Step 2 is searchable: a[b eq 
"c"]
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Path is fully searchable.
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Gathering constraints.
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:14: Comparison contributed hash 
value constraint: b = "c"
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Step 2 predicate 1 contributed 
1 constraint: b eq "c"
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:14: Comparison contributed hash 
value constraint: b = "c"
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Step 2 predicate 1 contributed 
1 constraint: b eq "c"
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Step 2 contributed 2 
constraints: a[b eq "c"]
2012-10-22 09:06:18.661 Info: 8003-cq: at 5:9: Comparison contributed dateTime 
range value constraint: dls:created < xs:dateTime("2012-12-12T00:00:00")
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Where clause contributed 1 
constraint for $n: $n/dls:created < xs:dateTime("2012-12-12T00:00:00")
2012-10-22 09:06:18.661 Info: 8003-cq: at 4:11: Executing search.
2012-10-22 09:06:18.667 Info: 8003-cq: at 4:11: Selected 1 fragment to filter

That looks exactly as expected (aside from the duplication of b=c, anyway). If 
we increment the date to the 13th or the 31st, both fragments match. If we try 
to use a QName that doesn't have a range index, we lose the "Comparison 
contributed dateTime range value constraint" line. So it seems clear that the 
range index is used for the where-clause.

Note that the same expression will be unsearchable according to xdmp:plan, 
though. Presumably that's because the FLWOR expression taken as a whole is 
unsearchable. That's understandable, but a little frustrating.

Even knowing this, I would still recommend doing as much as possible in XPath 
predicates. For one thing, you can use xdmp:plan instead of xdmp:query-trace.

-- Mike

On 22 Oct 2012, at 08:02 , David Sargeant <da...@dsargeant.com> wrote:

> I'm wondering if ML uses a range index in the where portion of a FLOWR 
> expression where applicable.  I could only find examples of 'order by'.  
> Thanks.
> 
> 
> 
> David
> 
> _______________________________________________
> General mailing list
> General@developer.marklogic.com
> http://developer.marklogic.com/mailman/listinfo/general

_______________________________________________
General mailing list
General@developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
General@developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to