Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine Objectify/AppEngine:计算查询返回的对象数的最佳方法?_Google App Engine_Objectify - Fatal编程技术网

Google app engine Objectify/AppEngine:计算查询返回的对象数的最佳方法?

Google app engine Objectify/AppEngine:计算查询返回的对象数的最佳方法?,google-app-engine,objectify,Google App Engine,Objectify,使用AppEngine上的Objectify计算查询返回的对象数(不实际加载)的最佳(即最有效)方法是什么?我猜最好的办法是取出所有的钥匙并计算结果: public int getEntityCount(Long v) { Objectify ofy = ObjectifyService.begin(); Iterable<Key<MyEntity>> list = ofy.query(MyEntity.class) .filter

使用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;
}
public int getEntityCount(长v){
Objectify of y=ObjectifyService.begin();
Iterable list=ofy.query(MyEntity.class)
.filter(“字段”,v).fetchKeys();
int n=0;
用于(键e:列表)
n++;
返回n;
}
似乎没有任何专门的方法来做到这一点。有什么想法吗?

找到了:

int n = Iterable<Key<MyEntity>> list = ofy().query(MyEntity.class)
      .filter("field", v).count();
int n=Iterable list=ofy().query(MyEntity.class)
.filter(“字段”,v.count();

它就是这么简单,尽管效率很高,因为它检索所有的键。最好将您的UI设计为处理未知数量的结果(例如谷歌,它提供了页面数量的线索,而不是实际数量)

请记住,计算实体的效率总是很低-O(n)时间。如果你认为会有很多,你应该保持一个持久的计数。虽然效率不高,但至少它不会获取实体,只获取键,对吗?在我的例子中,可以有很多“MyEntity”对象,但筛选器计数从来没有那么大(这种方法一次获取的项目数不是有问题吗?我尝试过类似的方法,但最终没有起作用,因为最大计数为1000(dev server)。您应该始终通过静态方法y()访问y并且不保留对它的引用。对于v4是这样,但问题是v3何时仍然是最新版本,并且该代码遵循v3文档编码风格。现在在v4中,这是一种不好的做法,但不是错误。。。