在 AppEngine 上使用 Objectify 计算查询返回的对象数量的最佳(即最有效)方法是什么,而不实际加载它们?我猜最好的办法是获取所有键并计算结果:
public int getEntityCount(Long v) {
Objectify ofy = ObjectifyService.begin();
Iterable<Key<MyEntity>> list = ofy.query(MyEntity.class)
.filter("field", v).fetchKeys();
int n = 0;
for (Key<MyEntity> e : list)
n++;
return n;
}
似乎没有任何专门的方法来做到这一点。有什么想法吗?
最佳答案
找到了:
int n = Iterable<Key<MyEntity>> list = ofy().query(MyEntity.class)
.filter("field", v).count();
就这么简单,但效率很高,因为它会检索所有 key 。最好将您的 UI 设计为处理未知数量的结果(例如 Google 会提供页数线索而不是实际页数)
关于google-app-engine - 对象化/AppEngine : best way to count # of objects returned by a query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043001/