Skip to content

Instantly share code, notes, and snippets.

@zeitstein
Created November 3, 2022 08:28
Show Gist options
  • Save zeitstein/8ba5c6c05583a75eee234e474e79d0ff to your computer and use it in GitHub Desktop.
Save zeitstein/8ba5c6c05583a75eee234e474e79d0ff to your computer and use it in GitHub Desktop.
Benchmarking various strategies for "absence queries"
(ns absenceq
(:require [xtdb.api :as xt]))
(def absnode (xt/start-node {}))
(def tx-data
(mapv (fn [n]
[::xt/put (cond-> {:xt/id n :even? (even? n)}
(= 0 (mod n 100))
(assoc :query? false))])
(range 1 10001)))
(xt/submit-tx absnode tx-data)
(xt/sync absnode)
(defn q-not-join []
(xt/q (xt/db absnode)
'{:find [e]
:where [[e :even? true]
(not-join [e] [e :query? false])]}))
(defn q-join []
(xt/q (xt/db absnode)
'{:find [e]
:where [[e :even? true]
(not [e :query? false])]}))
;; https://clojurians-log.clojureverse.org/xtdb/2021-11-03/1636464445.116200
(defn q-get-attr []
(xt/q (xt/db absnode)
'{:find [e]
:where [[e :even? true]
[(get-attr e :query?) n]
[(nil? n)]]}))
(= (q-not-join) (q-join) (q-get-attr))
(def absnode-explicit (xt/start-node {}))
(def tx-data-explicit
(mapv (fn [n]
[::xt/put {:xt/id n :even? (even? n) :query? (not= 0 (mod n 100))}])
(range 1 10001)))
(xt/submit-tx absnode-explicit tx-data-explicit)
(xt/sync absnode-explicit)
(defn q-explicit []
(xt/q (xt/db absnode-explicit)
'{:find [e]
:where [[e :even? true] [e :query? true]]}))
(= (q-get-attr) (q-explicit))
(time (dotimes [_ 10] (q-not-join))) ;; 2500
(time (dotimes [_ 10] (q-join))) ;; 2500
(time (dotimes [_ 10] (q-get-attr))) ;; 700
(time (dotimes [_ 10] (q-explicit))) ;; 900
;; not-join and join usually very close
;; get-attr consistently on par or faster than explicit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment