如何构建DynamoDB的键和索引结构。

huangapple go评论53阅读模式
英文:

How do I structure DynamoDB keys / indices

问题

我正在尝试在DDB中设置我认为是相对简单的结构,但我发现文档非常令人困惑,我的实验一直遇到问题。希望有人可以帮助我指出我应该如何构建这个结构。

我有三个不同的对象类别。每个类别都有一个唯一的静态ID号码;一个可以更改的非唯一日期标识符;以及一组其他属性(非唯一,可更改)。每个类别的其他属性集合都不同。

我需要能够通过ID检索对象;检索特定类别的所有对象;检索在日期范围内的所有对象;以及更新对象的日期和其他属性。以后可能需要按其他属性进行搜索。

在这种情况下,表格/键/二级索引的最佳结构是什么?

到目前为止,我尝试过以下方法:

  1. 单个表,哈希键 = 对象类型,范围键 = ID,全局二级索引 = 日期作为哈希键。

    • GSI不支持日期查找的“between”运算符。
      (KeyConditionExpression=Key('date').between(d_min, d_max))
  2. 每个类别一个表,哈希键 = ID,范围键 = 日期

    • 当我更新日期已更改的项目时,主键由ID和日期组成,导致出现第二个记录。
  3. 每个类别一个表,哈希键 = ID,没有范围键。GSI = 日期作为哈希键。

    • GSI不支持日期查找的“between”运算符。
  4. 每个类别一个表,哈希键 = ID,没有范围键。GSI = 日期作为范围键。

    • GSI需要一个哈希键。
英文:

I am trying to set up what I think is a relative simple structure in DDB, but I am finding the docs very confusing and my experiments keep running into problems. I'm hoping someone can help point me in the right direction on how I should structure this

I have three different object classes. Each has a unique, static id number; a non-unique date identifier that can change; and a set of other attributes (non-unique, changeable). The set of other attributes is different for each class

I need to be able to retrieve objects by id; retrieve all objects of a specific class; retrieve all objects within a range of dates; and update an object's date and other attributes. I may need to search by other attributes later.

What is the best way to structure tables / keys / secondary indices for this situation?

So far I have tried:

  1. A single table, hash key = object type, range key = id, GlobalSecondaryIndex = date as hash key.

    • GSI doesn't support 'between' operator for date lookups.
      (KeyConditionExpression=Key('date').between(d_min, d_max))
  2. One table for each class, hash key = id, range key = date

    • when I update an item whose date has changed, I end up with a second record as the primary key is composed of both id and date
  3. One table for each class, hash key = id, no range key. GSI = date as hash key

    • GSI doesn't support 'between' operator for date lookups.
  4. One table for each class, hash key = id, no range key. GSI = date as range key

    • GSI requires a hash key

答案1

得分: 1

  1. 我需要能够通过id检索对象;
  2. 检索特定类别的所有对象;
  3. 检索日期范围内的所有对象;
  4. 更新对象的日期和其他属性。

我以后可能需要按其他属性搜索 = 添加更多的GSI。

基本表格

满足用例1 + 2 + 4,假设在按id搜索时知道类别。

主键 辅助键 GSI主键 GSI辅助键
TYPECLASS TYPE#ID123 1 2023-07-06T00:00:000
TYPECLASS TYPE#ID233 1 2023-07-06T00:00:000
TYPECLASS TYPE#ID111 1 2023-07-06T00:00:000

GSI

满足用例4。这假定您的写入不会超过1000 WCU,如果会超过,您需要写入分片 GSIPK

GSI主键 GSI辅助键 主键 辅助键
1 2023-07-06T00:00:000 TYPECLASS TYPE#ID123
1 2023-07-06T00:00:000 TYPECLASS TYPE#ID233
1 2023-07-06T00:00:000 TYPECLASS TYPE#ID111
英文:
  1. I need to be able to retrieve objects by id;
  2. retrieve all objects of a specific class;
  3. retrieve all objects within a range of dates;
  4. update an object's date and other attributes.

I may need to search by other attributes later = add more GSI's.

Base Table

Fulfills use-case 1 + 2 + 4, assuming you know the class when searching by id.

PK SK GSIPK GSISK
TYPECLASS TYPE#ID123 1 2023-07-06T00:00:000
TYPECLASS TYPE#ID233 1 2023-07-06T00:00:000
TYPECLASS TYPE#ID111 1 2023-07-06T00:00:000

GSI

Fulfills use-case 4. This makes the assumption your writes won't exceed 1000 WCU, if they will, you will need to write shard the GSIPK

GSIPK GSISK PK SK
1 2023-07-06T00:00:000 TYPECLASS TYPE#ID123
1 2023-07-06T00:00:000 TYPECLASS TYPE#ID233
1 2023-07-06T00:00:000 TYPECLASS TYPE#ID111

huangapple
  • 本文由 发表于 2023年7月6日 18:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定