ndb.OR makes query costs more

自古美人都是妖i 提交于 2020-01-16 17:58:25

问题


Using AppEngine appstats I profiled my queries, and noticed that although the docs say a query costs one read, queries using ndb.OR (or .IN which expands to OR), cost n reads (n equals the number of OR clauses).

eg:

votes = (Vote.query(ndb.OR(Vote.object == keys[0], Vote.object == keys[1]))
     .filter(Vote.user_id == user_id)
     .fetch(keys_only=True))

This query costs 2 reads (it matches 0 entities). If I replace the ndb.OR with Vote.object.IN, the number of reads equals the length of array I pass to read.

This behavior is kind of contradicts the docs.

I was wondering if anyone else experienced the same, and if this is a bug in AE, docs, or my understanding.

Thanks.


回答1:


The query docs for ndb are not particularly explicit but this paragraph is your best answer

In addition to the native operators, the API supports the != operator, combining groups of filters using the Boolean OR operation, and the IN operation, which test for equality to one of a list of possible values (like Python's 'in' operator). These operations don't map 1:1 to the Datastore's native operations; thus they are a little quirky and slow, relatively. They are implemented using in-memory merging of result streams. Note that p != v is implemented as "p < v OR p > v". (This matters for repeated properties.)

In this doc https://developers.google.com/appengine/docs/python/ndb/queries



来源:https://stackoverflow.com/questions/23323886/ndb-or-makes-query-costs-more

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!