在 DynamoDB 中查询最新项目和正确使用分区键

Query for Latest Item & Proper Use of Partition Keys in DynamoDB

我正在创建 DynamoDB table 以支持用作播客播放器的 Alexa Skill。我设想 table 的方式是使用剧集编号作为分区键,使用 PublicationDate 作为可选的排序键。对于以这种方式设计我的 table 架构,我有两个顾虑。

首先,假设我想查询 table 以获取最新一集 - 我不确定我能否以这种方式完成,因为查询需要对分区键进行等价操作 ( episode = X),我事先不知道。如果播客有大量剧集(比如超过 1000 集),我认为扫描将是一项非常昂贵的操作是否正确?

我需要查看 table 中的每个项目,将其剧集编号(分区键值)与之前返回的项目进行比较,并在每次找到一个项目时用更新的项目更新一个变量,直到table 中的所有项目都以这种方式循环。

其次,DynamoDB 最佳实践说明了在我的用例中不一致的两件事(可能表明我的设计存在缺陷)。首先,分区键​​应该是唯一的或接近唯一的。其次,应该期望查询或多或少均匀地分散在键中。不过,就我而言,虽然分区键确实是唯一的,但我希望绝大多数查询都以 table 中的最新分区键为目标,以获取包含最新播客剧集数据的项目。例如,如果技能在任何给定的一天获得 1000 个查询,所有查询都针对单个分区键,会对性能产生什么影响?

有没有人对这种类型的数据有更好的table架构解决方案?

在此先感谢大家!

问题 1:

First, say I wanted to query the table to get the latest episode - I'm not sure that I can do it in this fashion, as a query requires an equivalence operation on the Partition Key (episode = X), which I wouldn't know in advance. Am I correct in believing that a scan would be quite an expensive operation if the podcast has a large number of episodes (say more than 1000)?

你是对的,你不能查询最新的剧集,因为每一集都在他们自己的分区中。分区几乎就像不同的孤立 tables,所以没有扫描就无法查询所有分区(如你所说)。

问题 2:

Secondly, DynamoDB best practices say two things which work incongruently in my use-case (probably a sign that my design is flawed). First, the Partition Key should be unique or close to unique. Second, queries should be expected to be more or less uniformly dispersed amongst the keys. In my case, though, while the Partition Key would indeed be unique, I would expect the vast majority of queries to be targeting the latest Partition Key in the table, for the Item containing data for the latest podcast episode. What would be the impact on performance if, say for example, the skill gets 1000 queries on any given day all aimed at a single Partition Key?

这里的问题有两个方面,AWS 希望您平等地(或接近平等地)读取(和写入)每个分区,所以基本上将要发生的事情是您将为写入单位(和Read Units)在你没有使用的分区上,即使你没有使用它们。

具体要运行多少取决于您查询数据库的次数,但是,正在阅读 便宜很多,而 1000 次读取在 table 1000 项上基本上是零。 IE。您可能可以摆脱它,但这并不理想。

备用Table架构/密钥设计

  1. 您还会进行哪些其他查询? IE。除了 "Check for latest Episode"
  2. 每天添加多少播客?星期?年?
  3. 是否有多个 'shows' 或类别可用于分布更均匀且可能 'known' 的分区键?