1

I'm new to dynamodb and trying to create a global secondary index for addition access patterns.

How can I create dynamoDB GSI with composite sort key?

For example: my primary table stored orders, which has these fields User_ID,ID, Type, Product_name, Total_Value, Created_at

On my primary table, the partition key is User_ID and sort key is Created_at, so I can get user's order history for the UI. My application also needs to get all orders by name, filtered by type, sorted by total_value and Created_at so I'm thinking of create a GSI with:

  • Parition key: Product_name
  • Sort key: Type#Total_value#Created_at

But when creating a new GSI I encountered this error:

Global Secondary Index range key not specified in Attribute Definitions.Type unknown.

So what should I do to create that GSI?

Quang
  • 135
  • 2
  • 9

1 Answers1

5

There is no automatic concatenation of values. You need to create an attribute for the GSI sort key (using any legal attribute name), construct a GSI referencing it, and put into it (via your code) the concatenated value you want there.

hunterhacker
  • 6,378
  • 1
  • 14
  • 11
  • Ah... I see. Damn this dynamo index is so complicated. What if later on, I need another access pattern, which sort key is another composite key that doesn't exist in the previous records? I guess I need to do some data migration or add that GSI attribute to older data – Quang Jul 05 '22 at 05:03
  • 1
    Correct. It’s easiest if you think of DynamoDB like a data structure your code uses which happens to be persisted within the cloud. How does something go somewhere? You put it there. Relational databases do a lot for you. This is convenient but also a reason why they don’t scale as well. – hunterhacker Jul 05 '22 at 16:20