简体   繁体   中英

AWS Dynamo db, choosing key

I am using dynamo db to store customer invoices where each invoice has its own unique identifier called 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. 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. 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. If you don't know the partition key, you need to scan (expensive operation - not recommended) the table or create 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.

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 - Partition Key
Invoice Id - Sort Key

GSI

Important Note:-

In a DynamoDB table, each key value must be unique. 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. For more details, please read about GSI.

Another important point, GSI will cost you. Create GSI only if you have an use case for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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