简体   繁体   English

如何更新ddb中的一对多关系

[英]how to update one to many relationship in ddb

Let's assume a bank and address example:让我们假设一个银行和地址示例:

class Address: 
    id: string
    street: string
    city: string

class BankBranch:
    id: string 
    name: string 
    employee_count: number
    address: object

Address is its own table because other services should be able to use instances of this Address table.地址是它自己的表,因为其他服务应该能够使用此地址表的实例。 For example each BankBranch has an address.例如,每个 BankBranch 都有一个地址。 Now when I store a BankBranch instance in DBB I'd imagine it will look like this:现在,当我在 DBB 中存储一个 BankBranch 实例时,我想它看起来像这样:

{
    name: "BranchXYZ",
    id: "123456",
    employee_count: 5,
    address: {
        street: "123 main st",
        city: "maincity",
        id: "456789"
    }
} 

and the address instance looks like this:地址实例如下所示:

{
    street: "123 main st",
    city: "maincity",
    id: "456789",
}

What's the best procedure to keep the data consistent?保持数据一致的最佳程序是什么? If I update the address instance, do I issue another call to the BankBranch and update it there as well?如果我更新地址实例,我是否会再次调用 BankBranch 并在那里更新它? So one update operation is actually 2 database operations?那么一个更新操作实际上是 2 个数据库操作?

Or instead of saving the entire address object I just save the id (like a foreign key) and then when I go I get request to the BankBranch instance I issue another request to the address issue?或者我不保存整个地址对象,而是只保存 ID(如外键),然后当我收到对 BankBranch 实例的请求时,我向地址问题发出另一个请求? That sounds a bit expensive.听起来有点贵。

You're asking if you should denormalize or not.你在问你是否应该去规范化。 That depends on many factors.这取决于许多因素。

First, are you doing this at a scale where it actually matters?首先,您是否在真正重要的规模上这样做? You say it's expensive.你说贵。 At highest price EC lookups are about 12.5c per million lookups.最高价格的 EC 查找大约是每百万次查找 12.5c。 How many secondary lookups would you really be doing?您真正要进行多少次二次查找?

Meanwhile you haven't explained in depth why the address has to be in another table.同时你还没有深入解释为什么地址必须在另一个表中。 Seems like it's an intrinsic part of the branch.似乎它是分支的固有部分。 You're really going to have other services referring to the address by some random ID number unrelated to the branch?您真的要让其他服务通过一些与分支机构无关的随机 ID 号引用该地址吗? Can't they refer to it by branch ID and pull out the address?他们不能通过分行 ID 引用它并提取地址吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在bigquery中连接两个具有一对多关系的表 - How to join two tables that has one to many relationship in bigquery 一对多关系的孩子抛出“一个或多个实例无法持久化” - One-to-many relationship children throw "One or more instances could not be made persistent" DynamoDB:避免在多对多关系中重复数据 - DynamoDB: Avoid duplicating data in many-to-many relationship 如何查找值是否存在,如果不存在则添加一个,如果存在则更新 - How to find if a value exit, add one if not exist or update if exist 如何将 Firestore 文档更新限制为仅在规则中增加一? - How to restrict Firestore document update to only increment by one in rules? 如何在 firestore 中拥有实时的多对多双向引用 - How to have realtime many to many two way references in firestore 如何建立从用户池到 aws appsync 模型的数据关系 - How to make a relationship of data from user pool to aws appsync model 如何更新 FCM 令牌 - How to update FCM token 在一个事务中更新多个文档,在多个集合中 Firestore - Update multiple doc, in multiple collection, in one transaction Firestore 如何更新 Firestore 中的数组项 - How to update an array item in Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM