在两个属性中优化ndb中的不等式查询 [英] Optimizing a inequality query in ndb over two properties

查看:226
本文介绍了在两个属性中优化ndb中的不等式查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对一系列有效日期进行查询

  q = Licence.query(Licence.valid_from < =今天,
Licence.valid_to> =今天,
ancestor = customer.key
).fetch(keys_only = True)

我知道Datastore不支持在两个属性上进行不等式查询。
所以我这样做:

  kl = Licence.query(Licence.valid_from <= today,
acestor = customer.key
).fetch(keys_only = True)
licenses = ndb.get_multi(kl)
用于许可证中的项目:
如果item.valid_to<今天:
licence.remove(item)

但我不喜欢,因为我认为我使用太多的RAM从数据存储库中检索更多实体(或密钥),而这些实体(或密钥)是我最终需要的。

任何机构都知道执行此类查询的更好方法吗?



足够在.get()之前使用.filter()?



谢谢

解决方案

一种解决方案是创建一个新的字段,例如start_week,它可以查询查询并允许您使用IN查询来过滤:

  q = Licence.query(范围(5,30)中的Licence.start_week,
Licence.valid_to> =今天,
ancestor = customer.key)

更简单:使用投影查询来识别正确的数据集,而无需获取完整的实体。这比普通的查询要快。

  it = Licence.query(License.valid_from <=今天,
ancestor = customer.key
).iter(projection = [License.valid_to])
keys = [e.key for e in it if if.valid_to> = today]
licenses = ndb.get_multi(键)


I'm trying to do a query into a range of valid dates

q = Licence.query(Licence.valid_from <= today, 
                  Licence.valid_to >= today,
                  ancestor = customer.key               
                  ).fetch(keys_only=True)

I know that Datastore doesn't support inequality queries over two propperties. So I do this:

kl = Licence.query(Licence.valid_from <= today, 
                  ancestor = customer.key               
                  ).fetch(keys_only=True)
licences = ndb.get_multi(kl)
for item in licences:
    if item.valid_to < today:
        licence.remove(item)

But I don't like because I think that I use too much RAM retrieving more entities (or keys) from the Datastore that I finally need.

Any body knows a better way of doing this type of queries?

Is enough to use .filter() before .get()?

Thanks

解决方案

One solution would be to create a new field, like start_week, which buckets the queries and allows you to use an IN query to filter:

q = Licence.query(Licence.start_week in range(5,30),
                  Licence.valid_to >= today,
                  ancestor = customer.key)

Even simpler: Use a projection query to identify the right set of data without fetching full entities. This is faster than a regular query.

it = Licence.query(License.valid_from <= today,
                   ancestor = customer.key
                   ).iter(projection=[License.valid_to])
keys = [e.key for e in it if e.valid_to >= today]
licenses = ndb.get_multi(keys)

这篇关于在两个属性中优化ndb中的不等式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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