# History db indexes sorted by Op-T?

**URL:** <https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770>\
**Category:** General\
**Created:** [January 3, 2019, 9:28pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770 "2019-01-03T21:28:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcgrue](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marcgrue/32/170_2.png) [@marcgrue](https://forum.datomic.com/u/marcgrue)\
**Post date:** [January 3, 2019, 9:28pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770/1 "2019-01-03T21:28:08Z")

</div>

When using a history database I found that indexes are ordered by the boolean asserted/retracted datom value before the T value. First by retractions, then assertions, or false-T, then true-T. I was surprised that my T’s were not in order by themselves. I haven’t been able to find this behaviour mentioned in the documentation and wonder what the rationale for it is?

Thanks,  
Marc

---

<div class="post-metadata">

**Author:** ![marshall](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marshall/32/48_2.png) [@marshall](https://forum.datomic.com/u/marshall)\
**Post date:** [January 10, 2019, 7:25pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770/2 "2019-01-10T19:25:19Z")

</div>

Marc,

Which index(es) specifically did you investigate (and I’m assuming you’re doing this investigation via the `datoms` API directly)?

I don’t believe any of the indexes make specific guarantees about the relative ordering of datoms based on the Op value.

---

<div class="post-metadata">

**Author:** ![marcgrue](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marcgrue/32/170_2.png) [@marcgrue](https://forum.datomic.com/u/marcgrue)\
**Post date:** [January 14, 2019, 12:46pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770/3 "2019-01-14T12:46:06Z")

</div>

Marshall,

I’ll try demonstrating by doing two transactions where the first one produces entity id 17592186045445 and the second updates that entity:

tx1  
[[:db/add, #db/id[:db.part/user -1000097], :ns/int, 1]]

tx2  
[[:db/add, 17592186045445, :ns/int, 2]]

Then when I call the datoms API on a history db:

```
conn.db().history().datoms(datomic.Database.EAVT, 17592186045445.asInstanceOf[Object]).forEach(d =>
  println(s"[${d.e} ${d.a} ${d.v} ${d.tx} ${d.added()}]")
)

```

I get the following 3 datoms:

```
[17592186045445 64 1 13194139534342 false]
[17592186045445 64 1 13194139534340 true]
[17592186045445 64 2 13194139534342 true]

```

whereas I would have expected the following order where the datoms are ordered by T (tx id) too:

```
[17592186045445 64 1 13194139534340 true]
[17592186045445 64 1 13194139534342 false]
[17592186045445 64 2 13194139534342 true]
```

---

<div class="post-metadata">

**Author:** ![marshall](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marshall/32/48_2.png) [@marshall](https://forum.datomic.com/u/marshall)\
**Post date:** [January 14, 2019, 2:17pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770/4 "2019-01-14T14:17:36Z")

</div>

Hi Marc,

The T is sorted in descending order.

This is a fairly “rare” case to see (i.e. you’d have to be using `datoms` on a history database), but I will see if I can add a description of this to the documentation.

---

<div class="post-metadata">

**Author:** ![marshall](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marshall/32/48_2.png) [@marshall](https://forum.datomic.com/u/marshall)\
**Post date:** [January 14, 2019, 4:06pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770/5 "2019-01-14T16:06:30Z")

</div>

Marc,

I’ve updated the docs:  
[https://docs.datomic.com/on-prem/indexes.html#basics](https://docs.datomic.com/on-prem/indexes.html#basics)

Thanks for pointing it out.

---

<div class="post-metadata">

**Author:** ![marcgrue](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marcgrue/32/170_2.png) [@marcgrue](https://forum.datomic.com/u/marcgrue)\
**Post date:** [January 14, 2019, 10:45pm UTC](https://forum.datomic.com/t/history-db-indexes-sorted-by-op-t/770/6 "2019-01-14T22:45:12Z")

</div>

Thanks for the clarification and docs update, Marshall.

If you allow me to be super-nitty-picky, then you might even consider making an addition to clarify that Datoms of the log (also an Index although not “one of four”) are sorted with T _ascending_. Maybe the first sentence of the log paragraph could be expanded to something like

“The log indexes datoms by time with transaction ids/T in ascending order.”

The Log feels naturally progressing in time for each transaction iterated whereas the descending order of Ts in the other four indexes (when using the history database) feels a bit like “three steps forward (E/A/V) and one step backward (T)”. From a user perspective I think it would be nice if Ts of the four indexes were instead sorted ascending to semantically align with the log.
