我有一个使用游标(Objectify v5)的数据存储查询,我希望在结果列表中的每一项之后获得游标。代码如下所示:
public List<Puzzle> queryWithCursor(String cursor, String order, int limit) {
Query<Puzzle> query = ObjectifyService.ofy()
.load()
.type(Puzzle.class)
.order(order)
.limit(limit);
query = query.startAt(Cursor.fromWebSafeString(cursor));
List<Puzzle> puzzles = new ArrayList<>();
QueryResultIterator<Puzzle> iterator = query.iterator();
while (iterator.hasNext()) {
Puzzle puzzle = iterator.next();
puzzle.setCursor(iterator.getCursor().toWebSafeString());
puzzles.add(puzzle);
}
return puzzles;
}
虽然该方法工作正常,但它在后台触发了如此多的数据存储查询。基本上,iterator.getCursor()每次运行时都会触发一个额外的查询。我从Stackdriver Trace中了解到,如果limit为20,则该方法总共触发19个查询(似乎最后一个.getCursor()不会触发额外的查询)。因此,这种方法比使用offset的类似查询速度更慢,成本更高。
这真的是一个bug吗?有没有办法避免对性能的影响?
https://stackoverflow.com/questions/51470091
复制相似问题