App Engine数据存储IN操作员 - 如何使用? [英] App Engine Datastore IN Operator - how to use?
问题描述
阅读: http://code.google.com/appengine /docs/python/datastore/gqlreference.html
我想用:
: = IN
但我不确定如何使其工作。让我们假设以下
class User(db.Model):
pre>
name = db.StringProperty()
class UniqueListOfSavedItems(db.Model):
str = db.StringPropery()
dateaved = db.DateTimeProperty()
$ b $ class UserListOfSavedItems(db.Model) :
name = db.ReferenceProperty(User,collection ='user')
str = db.ReferenceProperty(UniqueListOfSavedItems,collection ='itemlist')
我该如何做一个查询,让我获取用户保存的项目列表?很明显,我可以这样做:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' .name)
但是我得到了一个键列表。我想现在把这个列表拿到查询中,以便从UniqueListOfSavedItems中获取str字段。我认为我可以这样做:
pre $ q $ q $ = db.Gql(SELECT * FROM UniqueListOfSavedItems WHERE:= str in q)
但是有些东西不正确...有什么想法? (是在我的日常工作,所以现在不能测试):
q2 = db.Gql(SELECT * FROM UniqueListOfSavedItems __key__:= str中的q)
注意:搜索的是一个极其困难的问题因为我真正关心的是IN运算符。
既然你有一个键列表,需要做第二次查询 - 你可以做批量提取,试试这个:
#这应该让我成为用户保存的项目
useritems = db.get(saveditemkeys)
甚至不需要守卫子句 - 在0个实体上的db.get会被短路。)
有什么不同,你可能会问吗? get需要大约20-40ms,另一方面(不管是否使用GQL)的查询大约需要160-200ms,但是等等,情况会变得更糟!IN运算符用Python实现,并转换为多个查询,这些查询被执行所以如果你用一个IN过滤器对10个键进行查询,你可以做10个独立的160ms-ish查询操作,总共需要大约1.6秒的延迟。相比之下,单个db.get会产生相同的效果,总共需要大约30毫秒。
Reading: http://code.google.com/appengine/docs/python/datastore/gqlreference.html
I want to use:
:= IN
but am unsure how to make it work. Let's assume the following
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')
How can I do a query which gets me the list of saved items for a user? Obviously I can do:
q = db.Gql("SELECT * FROM UserListOfSavedItems WHERE name :=", user[0].name)
but that gets me a list of keys. I want to now take that list and get it into a query to get the str field out of UniqueListOfSavedItems. I thought I could do:
q2 = db.Gql("SELECT * FROM UniqueListOfSavedItems WHERE := str in q")
but something's not right...any ideas? Is it (am at my day job, so can't test this now):
q2 = db.Gql("SELECT * FROM UniqueListOfSavedItems __key__ := str in q)
side note: what a devilishly difficult problem to search on because all I really care about is the "IN" operator.
Since you have a list of keys, you don't need to do a second query - you can do a batch fetch, instead. Try this:
#and this should get me the items that a user saved
useritems = db.get(saveditemkeys)
(Note you don't even need the guard clause - a db.get on 0 entities is short-circuited appropritely.)
What's the difference, you may ask? Well, a db.get takes about 20-40ms. A query, on the other hand (GQL or not) takes about 160-200ms. But wait, it gets worse! The IN operator is implemented in Python, and translates to multiple queries, which are executed serially. So if you do a query with an IN filter for 10 keys, you're doing 10 separate 160ms-ish query operations, for a total of about 1.6 seconds latency. A single db.get, in contrast, will have the same effect and take a total of about 30ms.
这篇关于App Engine数据存储IN操作员 - 如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!