XQuery/Timing a Query


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


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


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()

  ($query-end-time - $query-start-time) div xs:dayTimeDuration('PT1S')

The results of this query will be something like:


This value is expressed in seconds.

Discussion & Other ApproachesEdit

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.)