在Google App Engine中执行大量IN查询的有效方法? [英] Efficient way to do large IN query in Google App Engine?

查看:84
本文介绍了在Google App Engine中执行大量IN查询的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户在他的移动设备上访问他的联系人。我想要将所有电话号码(比如250)发送回服务器,然后查询任何具有匹配电话号码的用户实体。

用户有一个索引的电话字段。所以我做了 User.query(User.phone.IN(phone_list)),但我只是看着AppStats,而且这很昂贵。这一操作耗费了250个读数,这是我期望用户经常进行的操作。



有哪些选择?我想我可以设置用户实体的ID值为他的电话号码(即当创建用户时,我会做 user = User(id = phone_number)),然后通过 ndb.get_multi(phones)直接获取密钥,但我也想通过电子邮件执行相同的查询。



有什么想法?

解决方案

您可以创建如下的PhoneUser模型:

  from google.appengine.ext import ndb 
$ b $ class PhoneUser(ndb.Model):
number = ndb.StringProperty()
user = ndb.KeyProperty()

class User(ndb.Model):
pass

u = User()
u.put ()

p = PhoneUser(id ='123-456-7890',number ='123-456-7890',user = u.key)
p.put()

u2 = User()
u2.put()

p2 = PhoneUser(id ='555-555-5555',number ='555-555-5555 ',user = u2.key)

result = ndb.get_multi([ndb.Key(PhoneUser,'123-456-7890'),ndb.Key(PhoneUser,'555-555-5555 )])

我认为这会适用于这种情况。每当您更新用户时,您只需添加/删除PhoneUser模型。您可以使用帖子挂钩来完成此操作: https://developers.google.com / appengine / docs / python / ndb / modelclass#Model__post_delete_hook


A user accesses his contacts on his mobile device. I want to send back to the server all the phone numbers (say 250), and then query for any User entities that have matching phone numbers.

A user has a phone field which is indexed. So I do User.query(User.phone.IN(phone_list)), but I just looked at AppStats, and is this damn expensive. It cost me 250 reads for this one operation, and this is something I expect a user to do often.

What are some alternatives? I suppose I can set the User entity's id value to be his phone number (i.e when creating a user I'd do user = User(id = phone_number)), and then get directly by keys via ndb.get_multi(phones), but I also want to perform this same query with emails too.

Any ideas?

解决方案

You could create a PhoneUser model like so:

from google.appengine.ext import ndb

class PhoneUser(ndb.Model):
  number = ndb.StringProperty()
  user = ndb.KeyProperty()

class User(ndb.Model):
  pass

u = User()
u.put()

p = PhoneUser(id='123-456-7890', number='123-456-7890', user=u.key)
p.put()

u2 = User()
u2.put()

p2 = PhoneUser(id='555-555-5555', number='555-555-5555', user=u2.key)

result =  ndb.get_multi([ndb.Key(PhoneUser, '123-456-7890'), ndb.Key(PhoneUser, '555-555-5555')])

I think that would work in this situation. You would just have to add/delete your PhoneUser model whenever you update your User. You can do this using post hooks: https://developers.google.com/appengine/docs/python/ndb/modelclass#Model__post_delete_hook

这篇关于在Google App Engine中执行大量IN查询的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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