I am building a DynamoDB table and am running into issues on how to best structure my indexes. I have 3 queries I need to perform.
My Table:
AttributeDefinitions:
# This is large groups that can have many events
- AttributeName: groupId
AttributeType: S
# An event can have many actions
- AttributeName: eventId
AttributeType: S
# Each item has a unique actionId
- AttributeName: actionId
AttributeType: S
# Each item has a creation date
- AttributeName: createdAt
AttributeType: S
# Some type I need to filter by (enum: trigger|task for example)
- AttributeName: actionType
AttributeType: S
# Main query to return items by action ID - that works fine
KeySchema:
- AttributeName: groupId
KeyType: HASH
- AttributeName: actionId
KeyType: RANGE
These are the 3 queries I need to achieve:
- Fetch a single item
Right now I do a getItem with
Key: {
groupId,
actionId
}
Works great.
- Fetch all items (actions) by eventId
SQL:
SELECT * FROM theTable WHERE eventId = 123
If I do this local index then that works great:
KeySchema:
- AttributeName: groupId
KeyType: HASH
- AttributeName: eventId
KeyType: RANGE
- Fetch all items with the actionType='trigger' ordered by createdAt date that belong to a groupId (irrespective of eventId)
SQL:
SELECT * FROM theTable WHERE actionType = 'trigger' AND groupId = 123 SORT BY createdAt
This is the one giving me issues. I would like to query my data and have it returned sorted by date. However I need to query using another field as my RANGE. So I if I add createdAt as my range I can't use actionType to filter. If I use actionType then there is no sort.
How can I best structure this table? In terms of amnount of data. There can be many groups (groupId). Each group can have many events (eventId). But each event is likely to only have <100 actions (actionId).