I’m new to DynamoDB and NoSQL architecture. I’m trying to figure out how to structure my keys in the most efficient way. AFAICT this means avoiding scans and only doing queries.
I have a set of records, and other records related to those in a many-to-many relation.
Reading documentation, the advised approach is to use
I’ll just ask whether you have a use case that fits a nosql database or are you trying to force it. I’ve seen customers drink the “we use dynamo for everything” cool aid but not realize that rearchitecture may be required and that Amazon has a scale that made it necessary.
Start with you data access storage, then patterns, then the db that fits (nosql, relational, whatever)
go with first but you need to add a second index that creates a parent lookup for you that would like what you have now. this will result in double writes
You can use a GSI. I’d create a model or type field to hold the parent type name as a string and use the GSI to find all records that have the GSI pk as model name and sort as the actual SK.
It sounds like you’re trying to used nosql for relational data. One thing which you’ll likely need to do is use the NextToken in the response to get all the items.
To answer your actual question. I’m making the assumption that you would want to get a child by first finding the parent.
pk: parent, sk:#123
pk: child, sk:#123#456
To return all parents you would do - pk: parent, sk: begins_with(“#”)
To return all children of parent 123 you would do - pk: child, sk: begins_with(“#123#”)
Write down your needed queries and try to estimate their usage. This will give you what should be table keys, local index(es) (if any), and global index(es).Your examples might work with added GSI.
The most used queries should hit table directly, don’t design the table primarily what’s easiest to write.
As someone mentioned: The DynamoDB book by Alex Debrie will be a great starting point to learn DynamoDB concepts.
Just choming in on single table design for dynamodb having just implemented accessed in Java - I think it suits JavaScript or python more than a strongly typed language. I spent a lot of time creating something that worked, don’t skip your integration tests: you really need them.