I'm just getting started with DynamoDB and NoSQL, and I'm a bit stuck on how to set up my key structure for maximum efficiency. I've read that the goal is to avoid using scans and focus on queries instead. I have a collection of records that are part of a many-to-many relationship, and I'm trying to design my keys accordingly.
Based on what I found in the documentation, it seems like using a structure like this is recommended:
pk sk attributes
--------------------------------------
Parent#123 Parent#123 {parent details}
Parent#123 Child#456 {child details}
I'm building an API that needs to retrieve a list of all parents, but I'm not sure how to query this table without resorting to scans. Currently, my pk/sk design looks like this:
pk sk attributes
--------------------------------------
Parent Parent#123 {parent details}
Parent#123 Child#456 {child details}
This setup allows me to query for the pk 'Parent', but I'm struggling with maintaining key integrity while inserting Child records. Any advice would be greatly appreciated!
2 Answers
You might want to check out Alex DeBrie's book on DynamoDB patterns. It's a great resource! Also, for the key integrity with adjacent records, it's best to manage that at the application layer or use condition expressions in your transactions. Here’s a link to his resources that can help you understand better: [Condition Expressions](https://www.alexdebrie.com/posts/dynamodb-condition-expressions/#1-confirming-existence-or-non-existence-of-an-item) and [TransactWriteItems](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html).
If you're worried about integrity, using a Global Secondary Index (GSI) could really help. You can define a model or type field for your parent items and sort the actual SK. This way, you can easily retrieve all related records without scanning. Just be ready for double writes, though!
You nailed it! Also, think about how you'll access your data. For instance, will you need to get all parents or maybe find children based on a specific parent ID? Planning your PK/SK structure around these access patterns is crucial. Just keep avoiding scans whenever you can!