Amazon DynamoDB与多对多关系 [英] Amazon DynamoDB and relationship many-to-many

查看:104
本文介绍了Amazon DynamoDB与多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,并且这些表之间的某些字段具有多对多关系。如何在Amazon DynamoDB中实现此功能?

解决方案

快速解答:在代码中或对表进行非规范化(存储冗余数据)。


  1. 去规范化;换句话说,将冗余数据存储在中央联接表中,这样您就可以通过查询该表来获取所需的所有信息。这可能是最好的选择,但是更新左或右表时,请记住要更新中央表中的所有相关记录。这样做的代价是额外的数据存储,以及每次更新都会带来额外的编程麻烦。


  2. 执行三个查询;拉入连接的中央表,然后向右和向左表再添加两个查询。


另外两个混合选项:


  1. 第三种选择是仅对一张表进行非规范化。左表上有一列,用于存储连接数据的serialized()数组。因此,您只需要拉入左表,您就已经拥有了从右表到的所有联接。如果您要更新右表,而又需要拉入所有左表,找到需要更新的位置,然后再更新左表,则不要使用这种方法。令人讨厌-因此您可能会喜欢第四个选项...


  2. 在左侧表格中存储相关ID的列表(以相同的序列化方式),因此您可以拉入左侧表格,取消IDS的序列化,并知道从右侧拉出什么。这意味着有两个查询,而不仅仅是三个查询。


哪个查询对您有用,实际上取决于程序,如果您只曾经添加数据或进行大量编辑和删除。如果它们都不起作用,请考虑使用关系数据库。






原因(很抱歉,如果您知道它,请添加



DynamoDB是一个noSQL数据库,它专为非关系查询而设计。因此,您不必执行JOINS以及外键和级联等操作。(是的,我在这里简化了,但希望可以将其概括起来)。您在查询中引入数据并在代码中执行 JOIN。



如果需要关系数据库,请使用关系数据库。





直接引用Amazon blurb:


Amazon DynamoDB解决了核心问题数据库
的可伸缩性,管理,性能和可靠性问题,它不是
关系数据库,并且不支持复杂的关系查询
(例如联接)或复杂的事务。如果您的工作负载需要此
功能,或者您正在寻找与现有
关系引擎的兼容性,请尝试使用Amazon Relational Database Service(RDS)。



I have two tables, and some fields between that tables has many-to-many relationships. How can i implement this feature in Amazon DynamoDB?

解决方案

Quick answer: Either in your code or denormalize your tables (store redundant data).

  1. Denormalize; in otherwords, store redundant data in the central "join" table so you can get all the information you want by querying that one table. This is probably your best option, but you need to remember to update all relevant records in the central table when you update the left or right tables. The cost of this is extra data storage, plus additional programming hassles for each update.

  2. Do three queries; pull in the central table of joins, then pull in with two more queries the right and left tables.

Two other hybrid options:

  1. The third option is to denormalise in one table only. On your left table have a column that stores a serialized() array of the join data. So you only need to pull in the left table nd you already have all the join into from the right table to. Don't do this approach if you update the right table much as you'd need to pull in all the left table, find where updates are needed and then update he left table. Nasty - so you could fall for the fourth option...

  2. Store a list of the related ids in the left table (in the same serialized manner), so you can pull in the left table, unseralise the IDS and know what to pull from the right. This means two queries and not just three.

Which one works for you really depends on the program and if you only ever "add" data or do lots of editing and deletions. If none of them work, then consider a relational database.


Reasons why (sorry if you know it, but adding for completeness).

DynamoDB is a noSQL database, which is designed for non-relational querying. So you don;t do JOINS and foreign keys and cascades etc. (Yes, I'm simplifying here, but hopefully that sums it up in a nutshell). You pull in data in queries and do the "JOIN"s in your code. Foreign keys and all that jazz are not not really in play.

If you want a relational database, then use a relational database.

To quote the Amazon blurb directly:

While Amazon DynamoDB tackles the core problems of database scalability, management, performance, and reliability, it is not a relational database and does not support complex relational queries (e.g. joins) or complex transactions. If your workload requires this functionality, or you are looking for compatibility with an existing relational engine, try Amazon Relational Database Service (RDS).

这篇关于Amazon DynamoDB与多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆