Reputation: 505
I have two tables in DynamoDB:
One article can have many tags such as "php", "html", "design" and "erlang".
I would like to query my articles table and paginate only through those that:
or
How would I do this most efficiently?
Upvotes: 0
Views: 1557
Reputation: 55760
Unfortunately the answer to your question doesn't really lie in the DynamoDB schema design. DynamoDB is not necessarily right/best tool for the job, though you can use it to model your problem.
Think of DynamoDB as a distributed, scalable hash-map. If you think of it this way you'll realize that the way you need to model the tags-to-articles mapping is by storing references to articles, keyed by the tag.
so perhaps:
articles table:
id (partition key),
title,
text,
tags
tags table:
tag (partition key),
article id (sort key)
Notice that the articles
table above has a list of tags that were applied to each article, and the same information is also stored in the tags
table. This goes against the best practices of normalizing your schema you might be familiar with from relational databases. But DynamoDB is a non-relational database so you have to structure your data differently.
It's important to mention that the schema as presented above will very likely lead to partition hot-spotting due to the fact that there will be some popular tags which will correspond to lots of articles, while the majority of tags will have very low cardinality. A potential strategy to deal with this problem might be to sub-partition the tag-space.
But you might have better results by combining DynamoDB with another technology, such as Elastic Search.
Upvotes: 2