与Python NDB一对多关系的有效实现 [英] Effective implementation of one-to-many relationship with Python NDB

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

问题描述

我想听听您对使用 Python NDB 有效实现一对多关系的看法.(例如人员(一个)到任务(多个))

I would like to hear your opinion about the effective implementation of one-to-many relationship with Python NDB. (e.g. Person(one)-to-Tasks(many))

据我所知,有三种实现方式.

In my understanding, there are three ways to implement it.

  1. 使用父"参数
  2. 使用重复"结构化属性
  3. 使用重复"键属性

我通常会根据以下逻辑选择一种方式,但对您来说有意义吗?如果你有更好的逻辑,请教我.

I choose a way based on the logic below usually, but does it make sense to you? If you have better logic, please teach me.

  1. 使用父"参数

  1. Use 'parent' argument

  • 这些实体之间需要进行交易操作
  • 这些实体之间需要双向引用
  • 强烈的亲子"关系

使用重复"结构化属性

  • 不需要单独使用多个"实体(始终与一个"实体一起使用)
  • 'many' 实体仅被 'one' 实体引用
  • 重复"的次数小于 100

使用重复"键属性

  • 需要单独使用多个"实体
  • 'many' 实体可以被其他实体引用
  • 重复"的次数超过 100

No.2 增加了实体的大小,但我们可以节省数据存储操作.(尽管我们需要使用投影查询来减少反序列化的 CPU 时间).因此,我尽量使用这种方式.

No.2 increases the size of entity, but we can save the datastore operations. (We need to use projection query to reduce CPU time for the deserialization though). Therefore, I use this way as much as I can.

非常感谢您的意见.

推荐答案

你遗漏的一个关键问题:你是如何阅读数据的?

A key thing you are missing: How are you reading the data?

如果您在请求中显示给定人员的所有任务,则 2 很有意义:您可以查询此人并显示他的所有任务.

If you are displaying all the tasks for a given person on a request, 2 makes sense: you can query the person and show all his tasks.

然而,如果你需要查询一个所有任务的列表,比如在某个时间到期,那么查询重复的结构化属性是很糟糕的.您需要为您的任务使用单独的实体.

However, if you need to query say a list of all tasks say due at a certain time, querying for repeated structured properties is terrible. You will want individual entities for your Tasks.

还有第四种选择,即在您的任务中使用指向您的人员的 KeyProperty.当您需要某人的任务列表时,您可以发出查询.

There's a fourth option, which is to use a KeyProperty in your Task that points to your Person. When you need a list of Tasks for a person you can issue a query.

如果您需要搜索单个任务,那么您可能想要使用 #4.您也可以将其与 #3 结合使用.

If you need to search for individual Tasks, then you probably want to go with #4. You can use it in combination with #3 as well.

此外,重复属性的数量与 100 无关.它与您的 Person 和 Task 实体的大小有关,以及有多少适合 1MB.这是潜在的危险,因为如果您的 Task 实体可能很大,则您的 Person 实体中的空间可能会比您预期的更快.

Also, the number of repeated properties has nothing to do with 100. It has everything to do with the size of your Person and Task entities, and how much will fit into 1MB. This is potentially dangerous, because if your Task entity can potentially be large, you might run out of space in your Person entity faster than you expect.

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

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