[英]DynamoDB Model Chronological Shipment Update Data
I recently just started learning about DynamoDB single table design.我最近刚开始学习 DynamoDB 单表设计。 Now, I am trying to model Shipment Update data that has the following properties:
现在,我正在尝试对具有以下属性的 Shipment Update 数据进行建模:
Access patterns:访问模式:
I am having a difficulty trying to resolve the 2 access patterns mentioned above.我在尝试解决上述两种访问模式时遇到了困难。 If, per se, I only have 1 record per shipment, then I can just update the sort key for the shipment update items to be
shpm#55abc
and the retrieval of all shipments for a given account by eta is straight forward, which is via the gsi accountEta
.如果,就其本身而言,我每个货件只有 1 条记录,那么我可以将货件更新项目的排序键更新为
shpm#55abc
,并且 eta 可以直接检索给定帐户的所有货件,即通过gsi accountEta
。
How do I resolve this to get the access patterns I need?如何解决此问题以获得所需的访问模式? Should I consider having a separate table for the shipment update audit, ie to store just the shipment updates?
我是否应该考虑为货件更新审核设置一个单独的表格,即仅存储货件更新? So that when I need access pattern #2, then I query this audit table by the shipment id to get all the chronological updates.
因此,当我需要访问模式 #2 时,我会通过货件 ID 查询此审计表以获取所有按时间顺序的更新。 But, I feel like this defeats the purpose of the single table design.
但是,我觉得这违背了单表设计的目的。
A single-table design is a good fit for these access patterns.单表设计非常适合这些访问模式。 Use overloadable, generic key names like PK and SK.
使用可重载的通用键名,如 PK 和 SK。 Here is one approach * :
这是一种方法* :
Shipments have a "current" record.装运有“当前”记录。 Add a global secondary index (
GSI1
) to create an alternate Primary Key for querying by account in ETA order (pattern #1).添加全局二级索引 (
GSI1
) 以创建备用主键,以便按 ETA 顺序按帐户查询(模式 #1)。 All changes to the shipment are executed as updates to this "current" record.对装运的所有更改都将作为对此“当前”记录的更新执行。
# shipment "current" record
PK SK GSI1PK GSI1SK
shpmt#55abc x_current account#123 x_eta#2022-07-01
Next, enable DynamoDB Streams on the table to capture shipment changes.接下来,在表上启用DynamoDB Streams以捕获发货更改。 Each time a "current" record is updated, the Lambda backing the Stream writes the
OLD_IMAGE
to the table as a change control record .每次更新“当前”记录时,支持 Stream 的 Lambda 会将
OLD_IMAGE
作为更改控制记录写入表。 This enables pattern #2 by shipment and account.这将通过发货和帐户启用模式#2。
# shipment update record
PK SK GSI1PK GSI1SK
shpmt#55abc update#2022-06-28T06:10:33.247Z account#123 update#2022-06-28T06:10:33.247Z
One virtue of this approach is that a single query operation can retrieve both the current shipment record and its full/partial change history in reverse order.这种方法的一个优点是单个查询操作可以以相反的顺序检索当前装运记录及其全部/部分更改历史记录。 This is the reason for the
x_
prefixes on the current record's keys.这就是当前记录的键上有
x_
前缀的原因。 A query with a key expression of PK = shpmt#55abc AND SK >= "update"
, DESC sorting with ScanIndexForward=False
and a limit of 2 returns the current record ( x_current
) and the latest update record.键表达式为
PK = shpmt#55abc AND SK >= "update"
的查询,使用ScanIndexForward=False
和限制为 2 的 DESC 排序返回当前记录 ( x_current
) 和最新的更新记录。
* Whether this is a good solution for you also depends on expected read/write volumes. * 这对您来说是否是一个好的解决方案还取决于预期的读/写量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.