![](/img/trans.png)
[英]Go + App Engine Datastore: How to filter out rows that are null?
[英]App Engine Datastore IN Operator - how to use?
阅读: http://code.google.com/appengine/docs/python/datastore/gqlreference.html
我想使用:
:= 输入
但我不确定如何让它发挥作用。 让我们假设以下
class User(db.Model):
name = db.StringProperty()
class UniqueListOfSavedItems(db.Model):
str = db.StringPropery()
datesaved = db.DateTimeProperty()
class UserListOfSavedItems(db.Model):
name = db.ReferenceProperty(User, collection='user')
str = db.ReferenceProperty(UniqueListOfSavedItems, collection='itemlist')
我如何进行查询以获取用户已保存项目的列表? 显然我可以这样做:
q = db.Gql("SELECT * FROM UserListOfSavedItems WHERE name :=", user[0].name)
但这让我得到了一个键列表。 我现在想获取该列表并将其放入查询中以从 UniqueListOfSavedItems 中获取 str 字段。 我以为我可以这样做:
q2 = db.Gql("SELECT * FROM UniqueListOfSavedItems WHERE := str in q")
但是有些不对劲……有什么想法吗? 是吗(在我的日常工作中,所以现在无法测试):
q2 = db.Gql("SELECT * FROM UniqueListOfSavedItems __key__ := str in q)
旁注:搜索起来真是一个非常困难的问题,因为我真正关心的是“IN”运算符。
由于您有一个键列表,因此您不需要进行第二次查询——您可以进行批量提取。 试试这个:
#and this should get me the items that a user saved
useritems = db.get(saveditemkeys)
(请注意,您甚至不需要 guard 子句 - 0 个实体上的 db.get 已适当短路。)
你可能会问,有什么区别? 好吧,一个 db.get 大约需要 20-40 毫秒。 另一方面,查询(无论是否使用 GQL)大约需要 160-200 毫秒。 但是等等,情况变得更糟,IN 运算符在 Python 中实现,并转换为多个查询。 它们是串行执行的,因此如果您使用 IN 过滤器查询 10 个键,您将执行 10 个单独的 160 毫秒左右的查询操作。 总共约有 1.6 秒的延迟。 相比之下,单个 db,get。 将具有相同的效果,总共需要大约 30 毫秒。
+1 给亚当,让我走上正轨。 根据他的指示,并在代码搜索中进行了一些搜索,我有以下解决方案。
usersaveditems = User.Gql(“Select * from UserListOfSavedItems where user =:1”, userkey)
saveditemkeys = []
for item in usersaveditems:
#this should create a list of keys (references) to the saved item table
saveditemkeys.append(item.str())
if len(usersavedsearches > 0):
#and this should get me the items that a user saved
useritems = db.Gql(“SELECT * FROM UniqueListOfSavedItems WHERE __key__ in :1’, saveditemkeys)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.