Separate tables vs map lists - DynamoDB

淺唱寂寞╮ 提交于 2021-01-28 19:02:12

问题


I need your help. I am quite new to databases.

I'm trying to get set up a table in DynamoDB to store info about TV shows. It seems pretty simple and straightforward but I am not sure if what I am doing is correct.

So far I have this structure. I am trying to fit everything about the TV shows into one table. Seasons and episodes are contained within a list of maps within a list of maps.

Is this too much layering? Would this present a problem in the future where some items are huge? Should I separate some of these lists of maps to another table?

Shows table


回答1:


Ideally, you should not put a potentially unbounded list in a single row in DynamoDB because you could end up running into the item size limit of 400kb. Also, if you were to read or write one episode of one show, you consume capacity as if you are reading or writing all the episodes in a show.

Take a look at the adjacency list pattern. It’s a good choice because it will allow you to easily find the seasons in a show and the episodes in a season. You can also take a look at this slide deck. Part of the way through, it talks about hierarchical data, which is exactly what you’re dealing with.

If you can provide more information about your query patterns, I can give you more guidance on how to model your data in the table.

Update (2018-11-26)

Based on your comments, it sounds like you should use composite keys to establish hierarchical 1-N relationships.

By using a composite sort key of DataType:ItemId where ItemId is a different format depending on the data type, you have a lot of flexibility. This approach will allow you to easily get the seasons in the show, get all episodes in all seasons, get all episodes in a particular season, or even get all episodes between season 1, episode 5 and season 2 episode 5.

hash_key  | sort_key        | data
----------|-----------------|----------------------------
SHOW_1234 | SHOW:SHOW_1234  | {name:"Some TV Show", ...
SHOW_1234 | SEASON:SE_01    | {descr:"In this season, the main character...
SHOW_1234 | EPISODE:S01_E01 | {...
SHOW_1234 | EPISODE:S01_E02 | {...

Here are the various key condition expressions for the queries I mentioned:

  • hash_key = "SHOW_1234" and sort_key begins_with("SEASON:") – gets all seasons
  • hash_key = "SHOW_1234" and sort_key begins_with("EPISODE:") – gets all episodes in all season
  • hash_key = "SHOW_1234" and sort_key begins_with("EPISODE:S02_") – gets all episodes in season 2
  • hash_key = "SHOW_1234" and sort_key between "EPISODE:S01_E5" and "EPISODE:S02_E5" – gets all episodes between season 1, episode 5 and season 2 episode 5


来源:https://stackoverflow.com/questions/53473339/separate-tables-vs-map-lists-dynamodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!