Friday, June 24, 2011

Using of XQuery index-of to implement a quick lookup service

Locating an element based on a value can be very expensive (full table scan each time)

this:
$retrievePtcarSummaryResponse1/ns0:RetrievePtcarSummaryResult/ns0:PtcarSummary[ns0:Id=$item]
is very slow if you have a lot of PtcarSummary.


I have tried to implement a lookup mechanism by building a sequence of the values being looked up:

let $mySequence := $retrievePtcarSummaryResponse1/ns0:RetrievePtcarSummaryResult/ns0:PtcarSummary/ns0:Id/text()


this will build a sequence made by all Id in all the PtcarSummary


then using

for $item in 1 to $myMax
        let $pos := fn:index-of($mySequence, xs:string($item))
    return
    if ($pos) then
    $retrievePtcarSummaryResponse1/ns0:RetrievePtcarSummaryResult/ns0:PtcarSummary[$pos]
    else
......


I was hoping to optimize the search.
Bummer.
I keep getting OutOfMemoryException for large (1600) sequences.

In fact I read here that sequences are quite poor in memory allocation.... bummer....

I was hoping XQuery was a SQL for XML, but it is not.
SQL support indexes - without them any DB would be stuck.
XQuery support for indexes is extremely limited - the language doesn't even support a HashMap, for Christ sake....

No comments: