[xquery-talk] Re: [x-query-talk] trying to optimize xpath expression

Frans Englich frans.englich at telia.com
Sat Aug 26 13:09:24 PDT 2006


On Friday 25 August 2006 20:34, Enric Jaen wrote:
> (Sorry if you receive this mail duplicated)
>
> Hi all,
>
> I'd like to improve the performance of the following simple xpath
> expression, used inside this simple XQuery function:
>
> declare function foo($input as node()) as item()+ {
>  for $elem in $input//EEE/FFF
>  return $elem
> };

First of all you can skip the for/return clause. I believe Saxon removes it 
all together, but for other implementations it can be an improvement. That 
is, the function can be:

declare function foo($input as node()) as item()+
{
	$input//EEE/FFF
};

It also looks like that the return type can be stricter. That is, element()+ 
instead of item()+.


>  I know that the input document has an structure like:
>
> <AAA>
>   <BBB>
>     <CCC>
>        <DDD>
>           <EEE>
> 	     <FFF>
> 	  <EEE>
> 	     <FFF>
> 	  <EEE>
> 	     <FFF>
> 	  ...
>       </DDD>
>     </CCC>
>   </BBB>
> </AAA>
>
> The names of  AAA, BBB, CCC, and DDD are unknown.
>
>
> I have tried changing the expression $input//EEE/FFF to:
>
> $input/*/*/*/*/EEE/FFF
>
> which I think it should perform better because the lack of the '//' path 
> (please correct me if I am wrong), however the time consumed is very
> similar.
>
> Similarly is happening with an XQuery function that performs:
> $input//EEE[FFF=$FFF]/FFF
>
> which I have changed it to:
> $input/*/*/*/*/EEE/[FFF=$FFF]FFF
>
> but the improvement is not that better.
>
> I am using saxonB8.7.3, and I am calling the XQuery function from Java. I
> am trying with 1000 <EEE> elements. The function is compiled into an
> XQueryExpression before to invoke the UserFunction.call method. The input
> document is a DocumentWrapper object.
>
> I wonder if there is something I can do to improve the performance of the
> Xpath expression?

I don't know if this holds for your input data(the above example snippet isn't 
well-formed, for example), but nevertheless:

$input/AAA[1]/BBB[1]/CCC[1]/DDD[1]/EEE/FFF

or simply

$input/AAA/BBB/CCC/DDD/EEE/FFF


Cheers,

		Frans


More information about the talk mailing list