I tried to do it in this way, here is query I've run: declare namespace lang = "http://s3r.ru/ns/code-language"; declare namespace t = "none"; declare variable $dbName := "test2"; declare function t:getIdByName($arg as xs:string) { let $x := db:attribute($dbName, $arg, 'name')/parent::object[ancestor::module] return if( empty($x/@id) ) then "-1" else $x/@id }; for $x in collection($dbName)/module//flow/argument/atom[@lang:tp = "function_call_name"]/@name let $id := t:getIdByName($x) return $id The collection 'test2' contains 100 documents (it is very small collection). The query executed 2017156.38 ms (about 30 minutes). This is too slow :( 2012/6/5 Christian Grün <christian.gruen@gmail.com>
declare function t:getIdByName($arg) { let $x := collection($dbName)/module//object[@name = $arg] return if( empty($x/@id) ) then "-1" else $x/@id };
Your query will be optimized if $dbName is globally declared (which I guess it is anyway), and if $arg is specified as string:
declare variable $dbName := '...'; declare function local:getIdByName($arg as xs:string) { let $x := collection($dbName)/module//object[@name = $arg] return if( empty($x/@id) ) then "-1" else $x/@id };
You may as well use db:attributes() to explicitly access the index (if available):
declare variable $dbName := '...'; declare function local:getIdByName($arg) { let $x := db:attribute($dbName, $arg, 'name')/parent::object[ancestor::module] return if( empty($x/@id) ) then "-1" else $x/@id };
Hope this helps, Christian