简体   繁体   English

AWS Dynamo db,选择密钥

[英]AWS Dynamo db, choosing key

I am using dynamo db to store customer invoices where each invoice has its own unique identifier called invoiceId . 我正在使用dynamo db存储客户发票,其中每个发票都有自己的唯一标识符,称为invoiceId

My read operations will be mainly getting invoices for a certain client. 我的阅读操作主要是为某个客户获取发票。

1- if I would use invoiceId as the partition key, invoices for the same client would probably be written to different partitions. 1-如果我将invoiceId用作分区键,则同一客户端的发票可能会写入不同的分区。 is this assumption correct and if it is should I worry about it? 这个假设是正确的吗?如果是,我应该担心吗?

2- Another scenario would be using client Id as the portioning key (grouping invoices for the same client in the partition, as much as possible), and use the invoiceId as sort key. 2-另一种情况是使用客户端ID作为分配密钥(尽可能将分区中同一客户端的发票分组),并使用invoiceId作为排序关键字。 Is that a correct approach or is there a better way to handle this scenario? 这是正确的方法还是有更好的方法来处理这种情况?

I am coming from a relational DB background, using a compound primary key where one component is redundant seems strange. 我来自关系数据库的背景,使用一个组件是多余的复合主键似乎很奇怪。

Point 1: Assumption is correct 第一点:假设是正确的

Point 2: It may not be the best approach because Dynamodb allows query operation on partition key only. 要点2:这不是最佳方法,因为Dynamodb仅允许对分区键进行查询操作。 If you don't know the partition key, you need to scan (expensive operation - not recommended) the table or create GSI. 如果您不知道分区键,则需要扫描(昂贵的操作-不推荐)表或创建GSI。

My proposed solution:- 我建议的解决方案:-

As your use case requires to get the data by Invoice id, having Invoice id as partition key is the best approach. 由于您的用例需要按发票ID获取数据,因此最好将Invoice id作为分区键。

Invoice id - Partition key 发票编号-分区键

In case if you have another use case to get all the invoices for the client id , you can create GSI (Global Secondary Index) with 如果您还有另一个用例来获取client id所有发票,则可以使用以下命令创建GSI(全局二级索引):

Client Id - Partition Key 客户端ID-分区密钥
Invoice Id - Sort Key 发票编号-排序键

GSI GSI

Important Note:- 重要的提示:-

In a DynamoDB table, each key value must be unique. 在DynamoDB表中,每个键值必须唯一。 However, the key values in a global secondary index do not need to be unique. 但是,全局二级索引中的键值不必唯一。

You can query the main table and GSI separately for your use case. 您可以针对您的用例分别查询主表和GSI。 For more details, please read about GSI. 有关更多详细信息,请阅读有关GSI的信息。

Another important point, GSI will cost you. 另一个重要的一点,GSI将花费您。 Create GSI only if you have an use case for it. 仅在有用例的情况下创建GSI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM