I have a MarkLogic 8 database in which there are documents which have two date time fields:

1. `created-on`
2. `active-since`

I am trying to write an Xquery to search all the documents for which the value of `active-since` **is less than** the value of `created-on`

Currently I am using the following FLWOR exression:

```xq
    for $entity in fn:collection("entities")

    let $id := fn:data($entity//id)
    let $created-on := fn:data($entity//created-on)
    let $active-since := fn:data($entity//active-since)

    where $active-since < $created-on

    return 
      (
        $id,
        $created-on,
        $active-since
      )

```

The above query takes too long to execute and with increase in the number of documents the execution time of this query will also increase. 

Also, I have 
`element-range-index` for both the above mentioned dateTime fields but they are not getting used here. The [cts-element-query](https://docs.marklogic.com/cts:element-range-query) function only compares one element with a set of atomic values. In my case I am trying to compare two elements of the same document.

I think there should be a better and optimized solution for this problem. 

Please let me know in case there is any search function or any other approach which will be suitable in this scenario.