XQuery performance very slow
591630Aug 16 2007 — edited Aug 29 2007Hello,
I have to query one document with a lot of similar elements.
Elements can have relationships among each other defined by a link between "id" and "href" attributes.
<ele1 id="0815">
<ele2>
<ele3 href="1234" />
</ele2>
<name>
<string>a name</string>
</name>
</ele1>
....
<ele1 id="5678">
<ele2>
<ele3 href="0815" />
</ele2>
<name>
<string>another name</string>
<scope>...</scope>
</name>
</ele1>
....
At the end there should for every "ele1" element following result row:
<result topic_id="5678" type_id="0815" topic_name="another name" type_name="a name" />
I tried to archive this by following XQuery:
declare function local:getName($id as xs:string) as xs:string
{
let $a := doc("doc.xml")/ele1[@id = $id]
let $not_scoped := $ele1/name/(if (not(scope)) then string else())
(: if we have "name" without "scope" element print this else print first "scope" :)
return if($not_scoped[1])
then string($not_scoped[1])
else (string($ele1/name[scope][1]/string))
};
for $topic in doc("doc.xml")/ele1[ele2/ele3]
let $topic_id := $ele1/@id
return <result topic_id='{$topic_id}' tpoic_name='{local:getName($topic_id)}' type_id='{$type_id}' type_name='{local:getName($type_id)}' />
The performance bottleneck is following part from "getName" function:
let $a := doc("doc.xml")/ele1[@id = $id]
It takes up to 60% of query time and pushes my query execution time behind 20sec for my test document.
Can anyone give my a hint or advice to optimize my query and boost execution time?
Thank you.
With kind regards,
squig