Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement two sort keys in Dynamo DB?

I’m building a database using DynamoDB on AWS.

I am using variable X as a partition key, and variable Y as a sort key.

I also have a variable Z which i need as a second sort key.

Is there a way to do this?

like image 632
Bob Avatar asked Feb 10 '26 19:02

Bob


2 Answers

Generally in DynamoDB you can create Local Secondary Indexes if you need alternative sort key:

To give your application a choice of sort keys, you can create one or more local secondary indexes on an Amazon DynamoDB table and issue Query or Scan requests against these indexes.

Important things to note are that LSIs can only be created when you create your main table, and they can't be deleted later.

You can have max 5 LSI per table.

like image 53
Marcin Avatar answered Feb 13 '26 18:02

Marcin


This is where you might use an LSI or GSI, but if this are queries you are not doing often and/or not at a high velocity, or the returned set with the PK and SK is small enough, you could just do a filter expression on the query results. Yes this is a little waste, but the cost of that waste could be less than the literal cost to having and maintain a secondary index. I cannot answer if this is the case for your workload though.

like image 20
Kirk Avatar answered Feb 13 '26 17:02

Kirk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!