尝试在DynamoDB中检索特定日期范围内的所有项目时,我可以避免进行扫描操作吗?

发布于 2025-01-30 06:21:18 字数 229 浏览 2 评论 0 原文

我有一个简单的表,该表包含一个唯一的分区键 ID 和许多其他属性,包括 date 属性。

现在,我想将所有记录在特定的时间范围内获取,但是据我所知,唯一的方法是使用扫描。

我尝试在 date 上使用GSI,但是我在 keyconditionexpression 中无法在之间使用。

还有其他选择吗?

I have a simple table which contains one unique partition key id and a bunch of other attributes including a date attribute.

I now want to get all records in a specific time range however as far as I understood, the only way to do this is to use a scan.

I tried to use a GSI on date but then I can not use BETWEEN in the KeyConditionExpression.

Is there any other option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2025-02-06 06:21:18

问:您是否提供一键<代码>分区密钥值?

答:如果,则可以查询。如果,则是扫描

您当前处于扫描区域,因为您需要在多个 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 can query. If NO, it's a scan.

You are currently in scan territory, because you need to search over multiple ids.

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 a PK and SK. Your PK could be YYYY-MM, for instance, depending on your query patterns. The SK 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文