XQuery/Timing a Query

      Motivation

      You want to time how long it takes for a query to run.

      Method

      We will bookend the query with two instances of the util:system-time() function.

      Example

      Our sample query that we want to time is as follows:

      for $x in (1 to 10000)
      return $x
      

      To time this query, we will insert util:system-time() on either end of the query:

      let $query-start-time := util:system-time()
       
      let $query := 
        for $x in (1 to 10000)
        return $x
       
      let $query-end-time := util:system-time()
       
      return
        ($query-end-time - $query-start-time) div xs:dayTimeDuration('PT1S')
      

      The results of this query will be something like:

      0.005
      

      This value is expressed in seconds.

      Discussion & Other Approaches

      Another approach to timing queries, using higher order functions, is discussed in the article on Timing Fibonacci algorithms.

      Also, eXist supports an XQuery pragma for timing queries, which logs the time required to complete a query.

      (Explain why util:system-time() is necessary and fn:current-time() won't work in this example.)

      Last modified on 14 March 2012, at 19:22