【发布时间】:2017-06-13 18:17:02
【问题描述】:
我正在开发一个 Web 系统,该系统的功能类似于 Twitter 的概念,即关注用户列表并将他们的帖子视为列表。
我想出的简单模型需要join 操作,这在datastore 中不可用。
class Post(Model):
author = reference to user id
content = text content
class Following(Model):
author = reference to user id
followed_by = reference to user id
频繁的操作是显示当前用户关注的用户的帖子列表(按时间排序)。
使用上述模型,只能分两步完成:
authors = Following.author when Following.followed_by == current_user
posts = Posts with Posts.author in authors
有什么方法可以更有效地做到这一点?
【问题讨论】:
-
就是这样。它需要 2 个查询。将第一个查询设为“仅键”查询以节省成本。
-
好的。谢谢。如果我们必须在
datastore和cloud-SQL之间进行选择,那么拇指规则应该是什么——比如“如果authors查询通常返回>200 个实体,那么切换到SQL是有意义的”。跨度> -
请注意
IN操作符也有问题,见cloud.google.com/appengine/docs/standard/python/ndb/…
标签: python google-app-engine google-cloud-datastore datastore nosql