Fork me on GitHub
#datomic
<
2022-04-18
>
hden05:04:37

How do I run effect range query on composite tuples? For example, consider the domain of course registrations, modeled with the following entity types:

[{:db/ident :course/id
  :db/valueType :db.type/string
  :db/cardinality :db.cardinality/one}
 {:db/ident :course/campus
  :db/valueType :db.type/ref
  :db/cardinality :db.cardinality/one}
 {:db/ident :course/created-at
  :db/doc "Timestamp stored as epoch millisec."
  :db/valueType :db.type/long
  :db/cardinality :db.cardinality/one}
 {:db/ident :course/campus+created-at
  :db/valueType :db.type/tuple
  :db/tupleAttrs [:course/campus :course/created-at]
  :db/cardinality :db.cardinality/one}]
Assuming there are lots of campus and courses, how do I effectively list all the courses created after a specific timestamp and is associated with a specific campus? (The query have to be dynamically generated so raw-index access is not a option) A
'[:find (pull ?course [...])
  :in $ ?campus-id ?timestamp
  :where
  [(tuple campus-id timestamp) ?tuple]
  [?course :course/campus+created-at ?index]
  [(>= ?index ?tuple)]
  [(untuple ?index) [?campus-id _]]]
B
'[:find (pull ?course [...])
  :in $ ?campus-id ?timestamp
  :where
  [?course :course/campus+created-at ?index]
  [(untuple ?index) [?campus-id ?created-at]]
  [(>= ?created-at ?timestamp)]]
ref: https://docs.datomic.com/cloud/schema/schema-reference.html#composite-tuples

favila12:04:27

A. (I’m assuming you are using cloud and have a value index?) B cannot make use of an index

favila12:04:57

Also, the real answer is “try both and see which is faster”. ;)

hden12:04:03

Got it. Thanks.