如何按日期(范围键)查询 DynamoDB,没有明显的哈希键? [英] How to query DynamoDB by date (range key), with no obvious hash key?

查看:29
本文介绍了如何按日期(范围键)查询 DynamoDB,没有明显的哈希键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使 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.

我能找到的最好的答案是用添加一个全局二级索引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:

  1. hash+range 需要是唯一的,而 day+timestamp(他推荐的方法)不一定是唯一的.
  2. 通过仅使用日期作为哈希值,我需要使用大量查询来获取自上次刷新日期(可能是几个月或几年前)以来每一天的结果.

因此,这是我采用的方法:

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 as id
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆