Bob
Bob

Reputation: 397

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?

Upvotes: 14

Views: 29403

Answers (2)

Marcin
Marcin

Reputation: 238827

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.

Upvotes: 12

NoSQLKnowHow
NoSQLKnowHow

Reputation: 4865

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.

Upvotes: 4

Related Questions