Hello,
unfortunately our queries are far from being simple and they require all parameters we pass. What I provided it's a mere trivial example of what we are facing.
Anyway, regarding function testFast1, what if I use the dynamic argument I pass, like:

declare function xbpr:testFast1($objectId as xs:string?){
let $docName := $cxnDefsDocName
let $conn_typeof_target := doc($docName)/CxnDefs/CxnDef[@type="RdfsType" and @source=$objectId]/@target
return $conn_typeof_target    
};

I thought it should fall into the same situation like testSlow but it does not, it still runs fast.

William

On 01/12/2011 10:43 AM, Leonard Wörteler wrote:
Hi William,

Am 12.01.2011 09:22, schrieb William Sandri:
We are migrating our product from Berkeley Xml DB to BaseX because of
overall best performances
nice to hear!

I already took a look at the query info but I obviously cannot say what
it is going on in the compilation process.
I looked into your query and I think I figured out what's going on:

In BaseX there are several optimization rules for preevaluating constant
subexpressions. The relevant ones are:
 - Function calls are evaluated if all arguments are evaluated
 - FLOWR constructs are eliminated if they don't use iteration

This leads to optimization of the first two functions, but not the last one:

declare function xbpr:testFast1($objectId as xs:string?){
  let $docName := $cxnDefsDocName
  let $conn_typeof_target := doc($docName)/CxnDefs
                             /CxnDef[@type="RdfsType"]/@target
  return $conn_typeof_target
};
Here the argument is dynamic, but it isn't used in the function body. So
the function body is preevaluated and calling the function is nearly
free performance-wise.


The second function
declare function xbpr:testFast2($docName as xs:string?){
  let $conn_typeof_target := doc($docName)/CxnDefs
                             /CxnDef[@type="RdfsType"]/@target
  return $conn_typeof_target
};
is called only with constant arguments, so it can be preevaluated
completely.


The slow function
declare function xbpr:testSlow($docName as xs:string?,
                               $objectId as xs:string?){
  let $conn_typeof_target := doc($docName)/CxnDef
                             /CxnDef[@type="RdfsType"]/@target
  return $conn_typeof_target
};
prevents both optimizations as it
 a) is called with the dynamic argument $objectId (which it ignores)
 b) depends on it's arguments, so the body cant be evaluated in advance

We'll refine the optimization of functions so that they are preevaluated
if all arguments are either constant or ignored.
Until then you could simply remove unused arguments from your functions
and/or separate constant subexpressions in other ways to help the optimizer.

I hope this helps,
  cheers Leo