1
votes

I have a dynamodb table. The table's partition key is id. One of its GSI has a partition key: type. When I create an item on dynamodb table, it requires me fill in the type field for GSI. Is it possible to make type as optional? That means there are some items I don't want them to be saved in GSI.

When I am using aws sdk client, I am able to put an item without the GSI partition key. It works as long as the item has table partition key. But when I work on AWS dyanmodb console, it always asks me to set GSI partition key. I think this is an issue with AWS console.

1

1 Answers

2
votes

You can create sparse indexes, but they are based on sort keys, not partition keys:

For any item in a table, DynamoDB writes a corresponding index entry only if the index sort key value is present in the item. If the sort key doesn't appear in every table item, the index is said to be sparse.

What's more GSI are sparse by default:

Global secondary indexes are sparse by default. When you create a global secondary index, you specify a partition key and optionally a sort key. Only items in the base table that contain those attributes appear in the index.

So back to your question, partition key is required. If you want to use sparse index, you can do this by having having GSI with type as a sort key.

This way, items that don't have type attribute, won't be present in sparse GSI.