【发布时间】:2019-07-21 05:54:04
【问题描述】:
我正在构建一个 DynamoDB 表,并且遇到了关于如何最好地构建我的索引的问题。我有 3 个查询需要执行。
我的桌子:
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
这些是我需要实现的 3 个查询:
- 获取单个项目
现在我用
做一个 getItemKey: {
groupId,
actionId
}
效果很好。
- 通过 eventId 获取所有项目(操作)
SQL:
SELECT * FROM theTable WHERE eventId = 123
如果我做这个本地索引,那么效果很好:
KeySchema:
- AttributeName: groupId
KeyType: HASH
- AttributeName: eventId
KeyType: RANGE
- 获取所有按 createdAt 日期排序的 actionType='trigger' 且属于 groupId 的项目(与 eventId 无关)
SQL:
SELECT * FROM theTable WHERE actionType = 'trigger' AND groupId = 123 SORT BY createdAt
这是给我的问题。我想查询我的数据并按日期排序返回。但是我需要使用另一个字段作为我的范围进行查询。因此,如果我将 createdAt 添加为我的范围,我将无法使用 actionType 进行过滤。如果我使用 actionType 则没有排序。
我怎样才能最好地构造这个表?在数据量方面。可以有很多组 (groupId)。每个组可以有许多事件 (eventId)。但是每个事件可能只有
【问题讨论】:
-
您现在使用的确切键条件表达式是什么?
-
@MatthewPope 您的问题帮助我更清楚地看到了我的问题 - 事实上,我可以在没有 createdAt 值的情况下进行查询,但不能使用我想要的过滤器。我已经编辑了问题。
-
那么您是在询问如何在 DynamoDB 查询中指定排序,还是在询问如何设计您的表以启用您想要的查询?如果是后者,将查询列出为英语句子或 SQL 可能会对您有所帮助。
-
我添加了一个 sql 来说明。我是否需要查询或索引结构方面的帮助取决于需要发生的事情。希望sql澄清一下?
-
这是您唯一需要做的查询吗?如果还有其他的,你能把它们也列出来吗?使用 DynamoDB,您可以获得出色的性能,但代价是您需要在设计表时考虑到所有访问模式。
标签: amazon-dynamodb dynamodb-queries