Michael Robinson
Michael Robinson

Reputation: 2127

DynamoDB Secondary Index on Multi-Value Type

I have a table that has a hash key of type Number and an attribute that is a multi-value set of numbers.

Assume the hash key is a group_id and the multi-value set is a set of members of that group (user_ids)

I want to be able to query Which groups is user X in?

I know I can easily do this by de-normalizing the data and also storing a table of user_ids to sets of group_id.

Before I do that, I want to be sure that there is no way to put a Secondary Global Index on to the multi-value attribute so that I can use one table and still get decent performance.

Upvotes: 1

Views: 2307

Answers (2)

mkobit
mkobit

Reputation: 47249

You can only create a GSI for a scalar-type attribute. The scalar type attributes are Number, String, Binary, Boolean, and Null.

From the Managing Global Secondary Indexes documentation:

Each index key attribute must be a scalar data type, not a multi-value set. You can project attributes of any data type into a global secondary index; this includes scalar data types and multi-valued sets. For a complete list of data types, see DynamoDB Data Types.

Upvotes: 2

Alexander Patrikalakis
Alexander Patrikalakis

Reputation: 5205

Given a base table schema of HashKey=group_id and RangeKey=user_id, you can add a GSI to with the reverse schema. The HashKey of the GSI would be user_id and the RangeKey would be group_id. This GSI will answer the question: "Which groups is user X in?". You do not need to create a second table and maintain it. In fact, DynamoDB will automatically propagate changes from your base table to a GSI, so you do not have to worry about keeping the index up to date.

Upvotes: 1

Related Questions