DynamoDB模型按时间顺序运输更新数据

发布于 2025-02-10 19:33:26 字数 657 浏览 1 评论 0 原文

我最近刚刚开始学习DynamoDB单桌设计。现在,我正在尝试建模具有以下属性的货运更新数据:

  • 一个帐户有多个用户
  • 一个帐户拥有多个货物的
  • 运输可以每次货运更新时多次更改ETA
  • ,将插入新记录,插入

访问模式:

  1. 获取所有货物的所有货物都以ETA的上升订单订购的最后更新状态,以
  2. 获得给定装运,获取按时间顺序更新

”在此处输入映像说明”

​上面提到的2个访问模式。如果本身本身,我每次发货只有1个记录,那么我只能更新货物更新项目的排序键,为 shpm#55abc ,以及ETA给定帐户的所有货物检索直截了当,这是通过GSI Accounteta

如何解决此问题以获取所需的访问模式?我应该考虑有一个单独的桌子来进行货运更新审核,即仅存储货运更新?因此,当我需要访问模式#2时,我通过货物ID查询此审核表以获取所有按时间顺序更新。但是,我觉得这打败了单个桌子设计的目的。

I recently just started learning about DynamoDB single table design. Now, I am trying to model Shipment Update data that has the following properties:

  • an account has multiple users
  • an account has multiple shipments
  • a shipment can change eta multiple times
  • each time there's a shipment update, a new record will be inserted

Access patterns:

  1. get all shipments of an account displaying the last updated status ordered by eta in an ascending order
  2. for a given shipment, get the chronological updates

enter image description here

enter image description here

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.

How do I resolve this to get the access patterns I need? Should I consider having a separate table for the shipment update audit, i.e. 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. But, I feel like this defeats the purpose of the single table design.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2025-02-17 19:33:26

单桌子设计非常适合这些访问模式。使用超负荷,*:

装运具有“当前”记录。添加一个全局辅助索引( gsi1 ),以创建一个以ETA顺序查询的替代主键(模式#1)。对货物的所有更改均以此“当前”记录的更新执行。

# shipment "current" record
PK             SK                                 GSI1PK            GSI1SK
shpmt#55abc    x_current                          account#123       x_eta#2022-07-01

接下来,启用 old_image 写入表格为更改控制记录。这可以通过运输和帐户启用模式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

这种方法的一种优点是,单个查询操作可以以相反的顺序检索当前的发货记录及其完整/部分变更历史记录。这就是当前记录键上 x _ 前缀的原因。具有 pk = shpmt#55abc和sk> =“ update” 的键表达式的查询( X_Current )和最新的更新记录。

*这对您来说是否是一个很好的解决方案,取决于预期的读/写入量。

A single-table design is a good fit for these access patterns. Use overloadable, generic key names like PK and 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). 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. Each time a "current" record is updated, the Lambda backing the Stream writes the OLD_IMAGE to the table as a change control record. This enables pattern #2 by shipment and account.

# 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. 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.

* Whether this is a good solution for you also depends on expected read/write volumes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文