尝试在DynamoDB中检索特定日期范围内的所有项目时,我可以避免进行扫描操作吗?
我有一个简单的表,该表包含一个唯一的分区键 ID
和许多其他属性,包括 date
属性。
现在,我想将所有记录在特定的时间范围内获取,但是据我所知,唯一的方法是使用扫描。
我尝试在 date
上使用GSI,但是我在 keyconditionexpression
中无法在之间使用。
还有其他选择吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问:您是否提供一键<代码>分区密钥值?
答:如果
是
,则可以查询
。如果否
,则是扫描
。您当前处于扫描区域,因为您需要在多个
ID
s上进行搜索。要获得应许的查询之地,请考虑DynamoDB的。一种实现是添加使用Compound
主键
代表日期的GSI。将日期分配在pk
和sk
之间。例如,您的pk
可以是yyyy-mm
,取决于您的查询模式。SK
将获得日期的剩余位(例如dd
)。覆盖日期范围将意味着在GSI上执行一个或几个查询。这种模式有许多变体。例如,如果比例尺是一个挑战,您主要是在查询最近几天的已知子集,则可以考虑将记录复制到配备上述键和TTL字段的单独报告表中以过期旧记录。与往常一样,“良好” dynamoDB解决方案集由您的查询模式和比例。
Q: Are you providing one-and-only-one
Partition Key
value?A: If
YES
, then you canquery
. IfNO
, it's ascan
.You are currently in scan territory, because you need to search over multiple
id
s.To get to the promised land of queries, consider DynamoDB's design pattern for time series data. One implementation would be to add a GSI with a compound
Primary Key
representing the date. Split the date between aPK
andSK
. YourPK
could beYYYY-MM
, for instance, depending on your query patterns. TheSK
would get the leftover bits of the date (e.g.DD
). Covering a date range would mean executing one or several queries on the GSI.This pattern has many variants. If scale is a challenge and you are mostly querying a known subset of recent days, for instance, you could consider replicating records to a separate reporting table configured with the above keys and a TTL field to expire old records. As always, the set of "good" DynamoDB solutions is determined by your query patterns and scale.