title: XQuery - Optimize a query for a BIG database
body: <p>I have a big xml database (30 000 files, 1.3 Go). One file in this database lists all the other files present in the database. My aim is "simply" to check if all files listed are present in the database. BUT I must not take care of the name of the files but only the XML code inside the documents.</p>
  
  <p>This is something like that :</p>
  
  <pre><code>declare variable $root :=  fn:collection();
  
  declare function local:isValid($fileCode) {
  
  let $fileSearchedIdentCode := $root/dmodule/identity/dmCode
  return 
  $fileCode/@attribute1 = $fileSearchedIdentCode/@attribute1 and
  $fileCode/@attribute2 = $fileSearchedIdentCode/@attribute2 and
  $fileCode/@attribute3 = $fileSearchedIdentCode/@attribute3
  
  };
  
  &lt;result&gt;
  { 
  for $fileCode in $root/file[identity/@fileType eq 'listOfFiles']/fileContent/fileEntry/fileCode 
  return
      if (local:isValid($fileCode))
      then   &lt;filePresent&gt;1&lt;/filePresent&gt;  
      else &lt;fileNonPresent&gt;2&lt;/fileNonPresent&gt;
  
  }
  &lt;/result&gt;
  </code></pre>
  
  <p>The code above is running for a small databse but for mine, it is requiring a incredible amount of time.</p>
  
  <p>SO, I wonder if someone can help me to improve that code in order to execute it in a reasonable time ;)</p>
  
  <p>(My database is indexed)</p>
  
  <p>Thanks for your help !!</p>
  
  <p>Johann</p>