DynamoDB: How to get unique values of a GSI partition key?

Viewed 2428

Say in AWS DynamoDB, I have a table:

ID (Partition key, GSI's Sort Key)   OtherAttribute      Name (GSI's Partition Key)
0                                       1/1/2020                Alice
1                                       2/1/2020                Bob
2                                       3/1/2020                John
3                                       4/1/2020                Alice
4                                       5/1/2020                Bob
5                                       5/1/2020                Michael

And my GSI projects all attributes of original table.

Now I want to finally get a Set data structure of {"Alice", "Bob", "John", "Michael"}.

How can I achieve this? It seems that Scan operation can not select unique values itself, which means the Scan operation in my case can not get any faster, right? Then after I get a list of all items retrieved, then I need to operate on this list to extract unique values of column Name, is this the only way to do this?

Thus I guess I need to scan the whole table. Here comes another question that is, since my GSI projects all attributes, will there be any difference by scanning GSI or just original table? My goal is certainly to achieve this job as fast as possible.

Could someone give some suggestions?

Thanks!

1 Answers

Dynamo doesn't support such operations.

You may want to reconsider the use of DDB.

The only way to accomplish what you want efficiently in dynamo is to enable streams and have a Lambda function that updates either another DDB table or another set of rows in your existing table...

You'd simply store a record with a partition key similar to "DISTINCT_NAME" and a sort key of :name.

This is basically the same solution used for Materialized Aggregation

Related