Secondary Index -DynamoDB

28 views Asked by At

I am new to dynamoDb and exploring its docs these days. I read about secondary index in dynamodb and have a small confusion. When we create a secondary index we specify partition and sort key (optional) for that index but do we also add attributes to secondary index which which we'll be querying using the secondary index?

Let's say, I have a table named Person and it contain attributes such as Name, Age, Gender, Contact, Star. It has name as the partition key and now I want to create a secondary index on Age such that I can query using Age and get the attributes - contact, star, name. Should I add these attributes (contact, star, name) to secondary index as well while creating it or I can just need to specify partition key (age in this case)?

1

There are 1 answers

0
Leeroy Hannigan On

You are only required to define the partition key Age in this case. By default, the projection will include ALL which means it will replicate the entire base table item (contact, star, name) into the index.

You can optionally choose to project only a subset of the attributes, (contact, star), for example.

The base table keys will always be projected, no matter if you exclude them from the Incudes projection type, no Name will always exist in the index at minimum.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Projection.html