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

查看:129
本文介绍了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.

我当前的计划如下:

应该可以通过指定时间范围和配置文件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.


  • 在一个表中实现此架构的好方法是什么? / li>
  • 使用当前设计是否有任何弊端?

  • 您将在当前设计中将什么指定为主索引/分区索引/排序键/二级索引和一个单表方法?

推荐答案

我要给出的答案是您需要能够执行以下查询。

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


  • 给一个帐户,找到所有配置文件

  • 提供个人资料,找到所有商品

  • 提供个人资料和特定的ItemType,找到所有商品

  • 提供个人资料,找到拥有的个人资料

  • 给出一个配置文件,找到拥有的帐户

  • Given an Account, find all profiles
  • Given a Profile, find all Items
  • Given a Profile and a specific ItemType, find all Items
  • Given an Item, find the owning Profile
  • Given a Profile, find the owning account

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}} key 相同。对于配置文件, pkey 的值也是 Account-{{uuid}} ,但 skey 的值为 Profile-{{uuid}} 。最后,对于某项, pkey Profile-{{uuid}} skey 项目-{{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}}以获取拥有的个人资料,与此类似,使用Profile可以获取拥有的帐户。

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天全站免登陆