简体   繁体   中英

How to efficiently query a list column in dynamodb?

I have a table that maps a parent key into multiple foreign keys. For example:

domain (hash) parent key (sort) foreign keys
1 A [B1, Y2, Z3]
1 X [B4, G6, Y9]

This structure is optimized for the most common workload, which is to lookup items based on a batch of parent keys using BatchGetItem with.eg. Keys = [{domain:1, parentKey:A}, {domain:1, parentKey:X}]

However, I also need to lookup parent keys with batches of foreign keys, eg in a single round-trip, find the parent keys for the foreign keys (Y2, B4, G6) => (A, X, X)

I cannot seem to find an efficient way to support this second workload in DynamoDB. The only idea I have is to run a Query with partition key (domain = 1), with no key condition on the sort key, and then use a Filter Expression to reduce down to relevant rows -- the problem is that this would consume the same capacity as if I were to retrieve all rows for (domain = 1), which would not work with my capacity model.

Any ideas on how I can achieve this second query format?

Bonus points: I also need a third, far less common workload, where I "getsert" parent keys. Similar to the second workload, however I insert a new table item if a parent key is not found.

You can copy the data.

FK (hash)   Parent (Sort)
B1          A
Y4          B

This way you will be able to efficiently look up the parents one by one. I will try to think of a way to make this into a single query

EDIT: I am not sure if it is possible but Dynamo has Batch Get Item that can query multiple partition keys at the same time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM