优化 ndb 中两个属性的不等式查询 [英] Optimizing a inequality query in ndb over two properties
问题描述
我正在尝试对有效日期范围进行查询
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)
我知道 Datastore 不支持对两个属性的不等式查询.所以我这样做:
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)
但我不喜欢,因为我认为从数据存储中检索更多我最终需要的实体(或键)时使用了太多 RAM.
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?
在 .get() 之前使用 .filter() 就足够了吗?
Is enough to use .filter() before .get()?
谢谢
推荐答案
一种解决方案是创建一个新字段,例如 start_week,它将查询分桶并允许您使用 IN 查询进行过滤:
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屋!