DynamoDB:什么时候使用什么PK类型? [英] DynamoDB: When to use what PK type?

查看:95
本文介绍了DynamoDB:什么时候使用什么PK类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读有关DynamoDB的最佳实践。我看到DynamoDB有两种PK类型:

I am trying to read up on best practices on DynamoDB. I saw that DynamoDB has two PK types:


  1. 哈希键

  2. 哈希和范围键

从我看来,后者似乎与前者类似,但支持对一组有限的列进行排序和索引。

From what I read, it appears the latter is like the former but supports sorting and indexing of a finite set of columns.

所以我的问题是为什么只使用哈希键而不使用范围键?

So my question is why ever use only a hash key without a range key? Is it a viable choice only when the table is not searched?

同样重要的是,对于何时使用哪种键类型有一些一般性的指导原则也很不错。我已经阅读了几本指南(包括Amazon自己的DynamoDB文档),但似乎都没有一个直接解决这个问题。

It'd also be great to have some general guidelines on when to use what key type. I've read several guides (including Amazon's own documentation on DynamoDB) but none of them appear to directly address this question.

谢谢

推荐答案

使用哪种密钥的选择下降针对特定场景的用例和数据需求。例如,如果要存储 User Session 数据,则使用 Range Key 可能没有多大意义,因为每个记录都可以由GUID引用并直接访问而无需分组。一般而言,一旦您知道会话ID,就可以通过键查询特定的项目。另一个示例可能是存储用户帐户或配置文件数据,每个用户都有自己的数据,您很可能将直接访问(通过用户ID或其他方式)。

The choice of which key to use comes down to your Use Cases and Data Requirements for a particular scenario. For example, if you are storing User Session Data it might not make much sense using the Range Key since each record could be referenced by a GUID and accessed directly with no grouping requirements. In general terms once you know the Session Id you just get the specific item querying by the key. Another example could be storing User Account or Profile data, each user has his own and you most likely will access it directly (by User Id or something else).

但是,如果要存储订单商品,则范围键会更有意义,因为您可能想检索按 Order 分组的项目。

However, if you are storing Order Items then the Range Key makes much more sense since you probably want to retrieve the items grouped by their Order.

根据数据模型哈希键可让您从表中唯一标识一条记录,和 Range Key (范围键)(可选)可以对通常一起检索的几条记录进行分组和排序。示例:如果您要定义一个汇总来存储 Order Items ,则 Order ID 可以是您的 Hash Key ,而 OrderItemId 范围键。每当您想从特定的 Order 中搜索 Order Items 时,只需通过哈希键(订单ID)查询,即可获得所有订单项目

In terms of the Data Model, the Hash Key allows you to uniquely identify a record from your table, and the Range Key can be optionally used to group and sort several records that are usually retrieved together. Example: If you are defining an Aggregate to store Order Items, the Order Id could be your Hash Key, and the OrderItemId the Range Key. Whenever you would like to search the Order Items from a particular Order, you just query by the Hash Key (Order Id), and you will get all your order items.

您可以在下面找到使用这两个键的正式定义:

You can find below a formal definition for the use of these two keys:


带有范围键的复合哈希键使开发人员可以创建
主键,该主键是两个属性( hash
属性和 range属性)的组合。 '在查询复合
键时,hash属性需要唯一匹配,但可以为range属性指定范围
操作:例如,过去24小时内来自Werner的所有订单
,或单个
玩家过去24小时内玩过的所有游戏。 [VOGELS]

因此范围键数据模型添加了分组功能,但是,这两个键的使用对存储模型也有影响:

So the Range Key adds a grouping capability to the Data Model, however, the use of these two keys also have an implication on the Storage Model:


Dynamo使用一致的散列法在其整个存储空间上划分其键空间
复制副本并确保统一的负载分布。假设
的密钥访问分布没有高度倾斜,统一的
分布可以帮助我们实现统一的负载分布。
[DDB-SOSP2007]

不仅哈希键可以唯一地标识记录,而且还可以确保负载分配。 Range Key (使用时)有助于指示大部分将一起检索的记录,因此,存储也可以针对此类需求进行优化。

Not only the Hash Key allows to uniquely identify the record, but also is the mechanism to ensure load distribution. The Range Key (when used) helps to indicate the records that will be mostly retrieved together, therefore, the storage can also be optimized for such need.

选择正确的密钥来表示数据是设计过程中最关键的方面之一,它直接影响应用程序的性能,规模和成本。

Choosing the correct keys to represent your data is one of the most critical aspects during your design process, and it directly impacts how much your application will perform, scale and cost.

脚注:


  • 数据模型是我们通过其进行建模的模型感知和操纵我们的数据。它描述了我们如何与数据库中的数据交互[FOWLER]。换句话说,这就是抽象数据模型,对实体进行分组的方式,选择作为主键的属性等的方式

  • The Data Model is the model through which we perceive and manipulate our data. It describes how we interact with the data in the database [FOWLER]. In other words, it is how you abstract your data model, the way you group your entities, the attributes that you choose as primary keys, etc

存储模型描述了数据库如何在内部存储和操作数据[FOWLER]。尽管您不能直接控制它,但是您可以通过了解数据库内部的工作方式来优化如何检索或写入数据。

The Storage Model describes how the database stores and manipulates the data internally [FOWLER]. Although you cannot control this directly, you can certainly optimize how the data is retrieved or written by knowing how the database works internally.

这篇关于DynamoDB:什么时候使用什么PK类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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