Skip to content

Instantly share code, notes, and snippets.

@dazld
Last active January 21, 2020 17:27
Show Gist options
  • Save dazld/a5ae3f0cb31e7dce5b1e5b02cb7e4ce2 to your computer and use it in GitHub Desktop.
Save dazld/a5ae3f0cb31e7dce5b1e5b02cb7e4ce2 to your computer and use it in GitHub Desktop.
datomic forum post draft

I'm trying to understand how to use the datoms API better - and doing so with a specific objective in mind, for optimising some sorting and aggregation queries.

Setup is as follows:

(require '[datomic.api :as d])

(def uri "datomic:mem://golfing")

(d/create-database uri)

(def conn (d/connect uri))

; schema 
@(d/transact conn [{:db/ident :rel/child
                    :db/valueType :db.type/ref
                    :db/index true
                    :db/cardinality :db.cardinality/many}
                   {:db/ident :std/name
                    :db/index true
                    :db/unique :db.unique/identity
                    :db/valueType :db.type/string
                    :db/cardinality :db.cardinality/one}])


; setup some data
; very ~250k-ish entities

(for [n (range 8000)]
  @(d/transact conn [{:std/name (str "parent-" n)
                      :rel/child (for [i (range (inc (rand-int 60)))]
                                   {:std/name (str "child-" i)})}]))

; querying!

; 300ms ish (8 core xeon)
(time (count (take 3 (->> (d/q '[:find [?e ...]
                                 :where 
                                 [?e :std/name]
                                 [?e :rel/child ?c]]
                               (d/db conn))
                          (map (partial d/entity (d/db conn)))
                          (sort-by (fn [e]
                                     (count (:rel/child e))))))))



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment