Rules vs Query performance

I have the following fn that returns a query using a pull pattern. It works.

(defn- site-query
  ([db site-id pattern cutoff]
   (d/q '[:find [(pull ?ci pattern) ...]
          :in $ pattern ?site-id ?cutoff
          :where
            [?ci :clockin/time ?t]
            [(> ?t ?cutoff)]
            [?ci :clockin/site ?site-id]]
        db
        pattern
        site-id
        cutoff))
  ([db site-id pattern]
   (site-query db site-id pattern (u/timestamp-days-ago 7))))

But if I refactor the query out into a rule, the result comes in a different order and takes 10 times longer. Once sorted though the results are equal.

(defn- site-query
  ([db site-id pattern cutoff]
   (d/q '[:find [(pull ?ci pattern) ...]
          :in $ % pattern ?site-id ?cutoff
          :where
            (testme ?ci ?cutoff ?site-id)]
        db
        ['[(testme ?ci ?cutoff ?site-id)
            [?ci :clockin/time ?t]
            [(> ?t ?cutoff)]
            [?ci :clockin/site ?site-id]]]
        pattern
        site-id
        cutoff))
  ([db site-id pattern]
   (site-query db site-id pattern (u/timestamp-days-ago 7))))

What gives?

1 Like