# Composite index

**URL:** <https://forum.datomic.com/t/composite-index/1414>\
**Category:** Datomic Applications\
**Created:** [April 10, 2020, 2:03am UTC](https://forum.datomic.com/t/composite-index/1414 "2020-04-10T02:03:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hden](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/hden/32/388_2.png) [@hden](https://forum.datomic.com/u/hden)\
**Post date:** [April 10, 2020, 2:03am UTC](https://forum.datomic.com/t/composite-index/1414/1 "2020-04-10T02:03:55Z")

</div>

Say that I’m working on a Google Calendar-like database where people store meetings.

1. A meeting have a start time and end time (mostly an hour or two).
2. A meeting can occupy multiple rooms.
3. Meetings might temporarily overlap (we don’t enforce strict collision control).

Assuming that a user often need to query things like “give me all the meeting today at this specific room(s)”, what’s the recommended index strategy?

## (01) R-tree

In PostgreSQL we might do

```
create table meetings (room int, during tstzrange);
create index meeting_index on meetings using gist (during);

```

But since Datomic does not have a GiST index (R-tree) so it’s not an option.

## (02) Composite Tuples

Since the rooms are limited and most of the meetings took place within an hour or two, we can cheat a little by creating a date index.

```
[{:db/ident :meeting/rooms
  :db/valueType :db.type/ref
  :db/cardinality :db.cardinality/many}
 {:db/ident : meeting/dates
  ;; Datomic does not support java.time.LocalDate so we have to go with strings
  :db/valueType :db.type/string
  ;; It's :db.cardinality/many since a meeting might take place at the midnight (yikes) and span two dates
  :db/cardinality :db.cardinality/many}]

```

But the query might be inefficient if we have lots of meeting (more than a million) within a day. So I thought maybe we can do

```
[{:db/ident :meeting/date-room-index
  :db/valueType :db.type/tuple
  :db/tupleAttrs [:meeting/dates :meeting/rooms]
  :db/cardinality :db.cardinality/many}]

```

Which trades storage space for query time, but Datomic does not allow :db.cardinality/many on composite tuples…

```
Execution error (ExceptionInfo) at datomic.client.api.async/ares (async.clj:58).
First error: :db.error/tuple-of-attrs-must-be-card-one

```

Ref: [https://portal.feedback.eu.pendo.io/app/#/case/113782](https://portal.feedback.eu.pendo.io/app/#/case/113782)

## (03) Hilbert Curves

I did find a report that implements R-Tree / Hilbert Curves on top of Datomic on-prem, but the codebase is no longer maintained.

> **[Sofra-SpatialIndexingForDatomic.pdf](https://slides.yowconference.com/yowlambdajam2014/Sofra-SpatialIndexingForDatomic.pdf)**
>
> 1137.96 KB

## (04) …

I have not thought of a elegant solution. Dear mighty Clojure / Datomic community, any thought?

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [April 16, 2020, 6:55pm UTC](https://forum.datomic.com/t/composite-index/1414/2 "2020-04-16T18:55:31Z")

</div>

Hi @hden. You can use _:db.type/instant_ instead of _:db.type/string_. _:db.type/instant_ is represented as UTC instant time.

Let’s assume you have two attributes: **:meeting/startLocalDate** and **:meeting/endLocalDate** as _db.type/instant_.

Then your query then might look:

```
[
:find ?meeting
:in $ ?roomRef ?lookedLocalDate
:where
   [?meeting :meeting/rooms ?roomRef]
   [?meeting :meeting/startLocalDate ?meetingStartDate]
   [?meeting :meeting/endLocalDate ?meetingEndDate]
   (or
       [(= ?meetingStartDate ?lookedLocalDate)]
       [(= ?meetingEndDate ?lookedLocalDate)]
   )
]
```

---

<div class="post-metadata">

**Author:** ![oscarlinusericsson](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/oscarlinusericsson/32/376_2.png) [@oscarlinusericsson](https://forum.datomic.com/u/oscarlinusericsson)\
**Post date:** [April 17, 2020, 3:18pm UTC](https://forum.datomic.com/t/composite-index/1414/4 "2020-04-17T15:18:03Z")

</div>

If you really need some kind of indexing which is not available/reasonable to implement within the datomic data model (without very much extra transactional data to keep the index up to date, that is), and you can keep an index process going, preferably in your local on-prem application, you can always keep an index as a separate datastructure and update it by the transaction queue, and feed it as data to your queries.

This may not be worth it in terms of extra complexity, but it is certainly one way to implement this feature. I guess the fulltext searching in Datomic is implemented this way.

And I realize I need to try this out myself.

---

<div class="post-metadata">

**Author:** ![hden](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/hden/32/388_2.png) [@hden](https://forum.datomic.com/u/hden)\
**Post date:** [April 18, 2020, 2:58am UTC](https://forum.datomic.com/t/composite-index/1414/5 "2020-04-18T02:58:46Z")

</div>

Thanks for the suggestion. Unfortunately for my case, I need to **atomically** query the index (in a Datomic Cloud instance) to determine if some transaction should be committed or rejected. If you could do that with an external index I’m all ears.
