[英]What is the most efficient way to do a ONE:ONE relation on google app engine datastore
Even with all I do know about the AppEngine datastore, I don't know the answer to this.即使我对 AppEngine 数据存储区了解的一切,我也不知道答案。 I'm trying to avoid having to write and run all the code it would take to figure it out, hoping someone already knows the answer.
我试图避免编写和运行所有代码来解决这个问题,希望有人已经知道答案。
I have code like:我有如下代码:
class AddlInfo(db.Model)
user = db.ReferenceProperty(User)
otherstuff = db.ListProperty(db.Key, indexed=False)
And create the record with:并使用以下命令创建记录:
info = AddlInfo(user=user)
info.put()
To get this object I can do something like:为了得到这个 object 我可以这样做:
# This seems excessively wordy (even though that doesn't directly translate into slower)
info = AddlInfo.all().filter('user =', user).fetch(1)
or I could do something like:或者我可以做类似的事情:
class AddlInfo(db.Model)
# str(user.key()) is the key to this record
otherstuff = db.ListProperty(db.Key, indexed=False)
Creation looks like:创作看起来像:
info = AddlInfo(key_name=str(user.key()))
info.put()
And then get the info with:然后通过以下方式获取信息:
info = AddlInfo.get(str(user.key()))
I don't need the reference_property in the AddlInfo, (I got there using the user object in the first place).我不需要 AddlInfo 中的 reference_property(我首先使用用户 object 到达那里)。 Which is faster/less resource intensive?
哪个更快/更少资源密集?
================== ===================
Part of why I was doing it this way is that otherstuff could be a list of 100+ keys and I only need them sometimes (probably less than 50% of the time) I was trying to make it more efficient by not having to load those 100+ keys on every request.....我这样做的部分原因是其他东西可能是 100 多个键的列表,而我有时只需要它们(可能不到 50% 的时间)我试图通过不必加载它们来提高效率每个请求都有 100 多个键.....
Between those 2 options, the second is marginally cheaper, because you're determining the key by inference rather than looking it up in a remote index.在这两个选项之间,第二个稍微便宜一些,因为您是通过推理来确定密钥,而不是在远程索引中查找它。
As Wooble said, it's cheaper still to just keep everything on one entity.正如 Wooble 所说,将所有东西都放在一个实体上仍然更便宜。 Consider an Expando if you just need a way to store a bunch of optional, ad-hoc properties.
如果您只需要一种方法来存储一堆可选的临时属性,请考虑使用Expando 。
The second approach is the better one, with one modification: There's no need to use the whole key of the user as the key name of this entity - just use the same key name as the User record.第二种方法更好,有一个修改:不需要使用用户的整个键作为该实体的键名 - 只需使用与用户记录相同的键名即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.