What is the best way to get only the count of NDB query result? (with less read operation)
获取NDB查询结果的最佳方法是什么? (少读取操作)
Which one is more efficient to get NDB query result count ? Normal Query or Projection Query ?:
哪一个更有效地获得NDB查询结果计数?普通查询或投影查询?:
EmailSent.query(EmailSent.unsub==True, EmailSent.mail_ref==mail_ref, projection=['to_email']).count()
EmailSent.query(EmailSent.unsub == True,EmailSent.mail_ref == mail_ref,projection = ['to_email'])。count()
EmailSent.query(EmailSent.unsub==True, EmailSent.mail_ref==mail_ref).count()
EmailSent.query(EmailSent.unsub == True,EmailSent.mail_ref == mail_ref).count()
I found same kind of question here: Get NDB query length - using Python on Google App Engine, but it's for getting the first query result.
我在这里找到了同样的问题:获取NDB查询长度 - 在Google App Engine上使用Python,但它是用于获取第一个查询结果。
2 个解决方案
#1
7
There is a count
operation.
有一个计数操作。
https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_count
https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_count
count(limit=None, **q_options)
count(限制=无,** q_options)
Returns the number of query results, up to a limit. This returns the same result as len(q.fetch(limit)) but more efficiently.
返回查询结果的数量,最多可达到限制。这将返回与len(q.fetch(limit))相同的结果,但效率更高。
#2
1
Use count
. Negligible differences in efficiency if you are able to paginate.
使用计数。如果您能够分页,效率可忽略不计。
list_of_emails = EmailSent.query(EmailSent.unsub==True)
total_count = list_of_emails.count()
offset = int(args['offset'])
limit = int(args['limit'])
list_of_emails, next_cursor, more = list_of_emails.fetch_page(limit, offset=offset)
prev_offset = max(offset - limit, 0)
prev = True if offset else False
next_ = True if more else False
next_offset = ''
if next_:
next_offset = offset + limit
objects = map(lambda emails: func(emails), list_of_emails)
return {'objects': objects, 'total_count': total_count, 'prev': prev, 'next': next_, 'prev_offset': prev_offset,
'next_offset': next_offset}
#1
7
There is a count
operation.
有一个计数操作。
https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_count
https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_count
count(limit=None, **q_options)
count(限制=无,** q_options)
Returns the number of query results, up to a limit. This returns the same result as len(q.fetch(limit)) but more efficiently.
返回查询结果的数量,最多可达到限制。这将返回与len(q.fetch(limit))相同的结果,但效率更高。
#2
1
Use count
. Negligible differences in efficiency if you are able to paginate.
使用计数。如果您能够分页,效率可忽略不计。
list_of_emails = EmailSent.query(EmailSent.unsub==True)
total_count = list_of_emails.count()
offset = int(args['offset'])
limit = int(args['limit'])
list_of_emails, next_cursor, more = list_of_emails.fetch_page(limit, offset=offset)
prev_offset = max(offset - limit, 0)
prev = True if offset else False
next_ = True if more else False
next_offset = ''
if next_:
next_offset = offset + limit
objects = map(lambda emails: func(emails), list_of_emails)
return {'objects': objects, 'total_count': total_count, 'prev': prev, 'next': next_, 'prev_offset': prev_offset,
'next_offset': next_offset}