DynamoDB 1 个大表还是多个小表? [英] DynamoDB 1 big table or multiple small tables?

查看:15
本文介绍了DynamoDB 1 个大表还是多个小表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前面临一些关于我的数据库设计的问题.目前我正在开发一个允许用户执行以下操作的 api:

I'm currently facing some questions regarding my database design. Currently i'm developing an api which lets users do the following:

  • 创建一个帐户(1 个用户拥有 1 个帐户)
  • 创建个人资料(1 个帐户拥有 1-n 个个人资料)
  • 让个人资料上传 2 种类型的项目(1 个个人资料拥有 0-n 个项目;这些项目的类型和用途不同)

调用 API 方法会触发 AWS Lambda 在 DynamoDB 表中执行请求的操作.

Calling the API methods triggers AWS Lambda to perform the requested operations in the DynamoDB tables.

我目前的计划是这样的:

My current plan looks like this:

应该可以通过指定时间范围和配置文件 ID 来查询项目.但我认为我的设计完全违背了 DynamoDB 的目的.AWS 文档说,一个设计良好的产品只需要一个表.

It should be possible to query items by specifying a time frame and the Profile ID. But i think my design completely defeats the purpose of DynamoDB. AWS documentation says that a well designed product only requires one table.

  • 在一张表中实现这种架构的好方法是什么?
  • 使用当前设计有什么缺点吗?
  • 在当前设计和单表方法中,您会指定什么作为主/分区/排序键/辅助索引?

推荐答案

假设您需要能够执行以下查询,我将给出这个答案.

I’m going to give this answer assuming that you need to be able to do the following queries.

  • 给定一个帐户,查找所有个人资料
  • 给定个人资料,查找所有项目
  • 给定一个 Profile 和一个特定的 ItemType,查找所有 Items
  • 给定一个项目,找到拥有的个人资料
  • 给定个人资料,找到所有者帐户

DynamoDB 的优点之一(也许也是一个祸根)是它主要是无模式的.您需要为表中的每个项目具有强制性的主键属性,但所有其他属性都可以是您喜欢的任何属性.为了让 DynamoDB 设计只有一张表,您通常需要习惯在同一张表中包含混合类型的对象的想法.

One of the beauties of DynamoDB (and also a bane, perhaps) is that it is mostly schema-less. You need to have the mandatory Primary Key attributes for every item in the table, but all of the other attributes can be anything you like. In order to have a DynamoDB design with only one table, you usually need to get used to the idea of having mixed types of objects in the same table.

话虽如此,这是您的用例的可能架构.我的建议假设您使用 UUID 之类的东西作为标识符.

That being said, here’s a possible schema for your use case. My suggestion assumes that you are using something like UUIDs for your identifiers.

分区键是一个简单地称为pkey(或任何你想要的)的字段.我们还将调用排序键 skey(但同样,这并不重要).现在,对于一个帐户,pkey 的值是 Account-{{uuid}} 并且 skey 的值将是相同的.对于 Profile,pkey 值也是 Account-{{uuid}},但 skey 值是 Profile-{{uuid}}.最后,对于一个 Item,pkeyProfile-{{uuid}}skeyItem-{{type}}-{{uuid}}.对于一个项目的所有属性,不用担心,只要使用你想使用的任何属性.

The partition key is a field that is simply called pkey (or whatever you want). We’ll also call the sort key skey (but again, it doesn’t really matter). Now, for an Account, the value of pkey is Account-{{uuid}} and the value of skey would be the same. For a Profile, the pkey value is also Account-{{uuid}}, but the skey value is Profile-{{uuid}}. Finally, for an Item, the pkey is Profile-{{uuid}} and the skey is Item-{{type}}-{{uuid}}. For all of the attributes of an item, don’t worry about it, just use whatever attributes you want to use.

由于父"对象始终是分区键,因此您只需查询父对象的 ID 即可获取任何子"对象.例如,获取配置文件的所有ItemType2"的关键条件表达式将是

Since the "parent" object is always the partition key, you can get any of the "child" objects simply by querying for the ID of the of the parent. For example, your key condition expression to get all the ‘ItemType2’s for a Profile would be

pkey = "Profile-{{uuid}}" AND begins_with(skey, "Item-Type2")

在此架构中,您的 GSI 具有与表相同的键,但相反.您可以在 GSI 中查询Item-{{type}}-{{uuid}}"以获取拥有的配置文件,类似地使用配置文件来获取拥有的帐户.

In this schema, your GSI has the same keys as the table, but reversed. You can query the GSI for ‘Item-{{type}}-{{uuid}}’ to get the owning Profile, and similarly with a Profile is to get the owning account.

我在这里说明的是 邻接列表模式.DynamoDB 还有一篇文章描述了如何使用 复合排序键分层数据,这也适用于您的数据,并且根据您的预期查询,它可能比使用邻接列表更合适.

What I have illustrated here is the adjacency list pattern. DynamoDB also has an article describing how to use composite sort keys for hierarchical data, which would also be suitable for your data, and depending on your expected queries, it might be more suitable than using the adjacency list.

您不必将所有内容都放在一个表中.是的,DynamoDB 推荐它,但更重要的是确保您的应用程序正确且可维护.如果拥有多个表意味着更容易编写无缺陷的应用程序,那么使用多个表.

You don’t have to put everything in a single table. Yes, DynamoDB recommends it, but it is far more important to make sure that your application is correct and maintainable. If having multiple tables means it’s easier to write a defect free application, then use multiple tables.

这篇关于DynamoDB 1 个大表还是多个小表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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