Let's say I have a kind named 'audit' and it has the following entries:
| tenantId | traceId | eventId |
|---|---|---|
| tenant1 | traceId1 | event1 |
| tenant1 | traceId1 | event2 |
| tenant1 | traceId2 | event3 |
| tenant1 | traceId2 | event4 |
I need to get all the rows unique on traceId whichever is the first entry so the query should result in:
| tenantId | traceId | eventId |
|---|---|---|
| tenant1 | traceId1 | event1 |
| tenant1 | traceId2 | event3 |
For the above, I am using select distinct on(traceId) * from audit
Even though it's a simple query, my concern is the performance of this query as my entries grow. I will have hundreds of thousands of entries in the datastore, but out them, 50% might be unique on traceId.
I have read datastore is not for aggregations. So, my questions are:
- Is distinct on considered an aggregation query?
- Does distinct on work on index scan?
- Will distinct on increase my read cost?
- Will the built-in index handle the distinct on or should we define a composite index?