如何按日期(范围键)查询DynamoDB,而没有明显的哈希键? [英] How to query DynamoDB by date (range key), with no obvious hash key?
问题描述
我需要使iOS应用上的本地数据与DynamoDB表中的数据保持同步。 DynamoDB表大约有2K行,只有一个哈希键( id
),并且具有以下属性:
I need to keep local data on an iOS app in sync with data in a DynamoDB table. The DynamoDB table is ~2K rows, with only a hash key (id
), and the following attributes:
-
id
(uuid) -
lastModifiedAt
(时间戳记) -
名称
-
纬度
-
经度
id
(uuid)lastModifiedAt
(timestamp)name
latitude
longitude
我目前正在通过 lastModifiedAt
进行扫描和过滤,其中 lastModifiedAt
大于应用程序的上次刷新日期,但是我想将会变得昂贵。
I am currently scanning and filtering by lastModifiedAt
, where lastModifiedAt
is greater than the app's last refresh date, but I imagine that will become expensive.
最好的 answer 我可以找到的是添加一个以 lastModifiedAt
作为范围的全局二级索引,但是GSI没有明显的哈希键。
The best answer I can find is to add a Global Secondary Index with lastModifiedAt
as the range, but there is no obvious hash key for the GSI.
当需要使用GSI按范围查询但没有明显的哈希键时的最佳做法是什么?或者,如果唯一的选择是全扫描,是否有最佳实践要降低成本吗?
What is best practice when needing to query by range using a GSI, but there is no obvious hash key? Alternatively, if a full scan is the only option, are there any best practices to keep down the cost?
推荐答案
虽然D.Shawley的答案为我指明了正确的方向,但它缺少两个考虑因素一个GSI:
While D.Shawley's answer helped point me in the right direction, it missed two considerations for a GSI:
- 散列+范围必须是唯一的,但是day + timestamp(他推荐的方法)不一定是唯一的。 / li>
- 通过仅使用日期作为哈希,我将需要使用大量查询来获取自上次刷新日期(可能是几个月或几年前)以来每一天的结果
因此,这是我采用的方法:
As such, here is the approach I took:
- 创建了一个全球二级索引(GSI),其哈希键为
YearMonth
(例如,201508
),范围为id
- 多次查询GSI,自最近一次刷新日期起,每个月查询一次。使用
lastModifiedAt>过滤查询。 [给定时间戳记]
。
- Created a Global Secondary Index (GSI) with the hash key as
YearMonth
(e.g.,201508
) and range asid
- Query the GSI multiple times, one query for each month since the last refresh date. The queries are also filtered with
lastModifiedAt > [given timestamp]
.
这篇关于如何按日期(范围键)查询DynamoDB,而没有明显的哈希键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!