XQuery/Timing a Query

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

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

Method[edit | edit source]

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

Example[edit | edit source]

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[edit | edit source]

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