如何在不影响性能的情况下获取 App Engine 数据存储区查询中每个实体的游标?

How to get the cursor of each entity in a App Engine datastore query without performance hit?

我有一个使用游标 (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() 不会触发额外的查询)。因此,这种方法比使用偏移量的类似查询更慢且成本更高。

这真的是一个错误吗?有没有办法避免性能下降?

import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.QueryResultList;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListPeopleServlet extends HttpServlet {

  static final int PAGE_SIZE = 15;
  private final DatastoreService datastore;

  public ListPeopleServlet() {
    datastore = DatastoreServiceFactory.getDatastoreService();
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);

    // If this servlet is passed a cursor parameter, let's use it.
    String startCursor = req.getParameter("cursor");
    if (startCursor != null) {
      fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
    }

    Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
    PreparedQuery pq = datastore.prepare(q);

    QueryResultList<Entity> results;
    try {
      results = pq.asQueryResultList(fetchOptions);
    } catch (IllegalArgumentException e) {
      // IllegalArgumentException happens when an invalid cursor is used.
      // A user could have manually entered a bad cursor in the URL or there
      // may have been an internal implementation detail change in App Engine.
      // Redirect to the page without the cursor parameter to show something
      // rather than an error.
      resp.sendRedirect("/people");
      return;
    }

    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter w = resp.getWriter();
    w.println("<!DOCTYPE html>");
    w.println("<meta charset=\"utf-8\">");
    w.println("<title>Cloud Datastore Cursor Sample</title>");
    w.println("<ul>");
    for (Entity entity : results) {
      w.println("<li>" + entity.getProperty("name") + "</li>");
    }
    w.println("</ul>");

    String cursorString = results.getCursor().toWebSafeString();

    // This servlet lives at '/people'.
    w.println("<a href='/people?cursor=" + cursorString + "'>Next page</a>");
  }
}

这实际上是数据存储的基本行为,至少在旧的 sdk 中是这样(与 Objectify 6 使用的新 sdk 不同,后者可能相同也可能不同)。在非批边界调用 getCursor() 会重新启动查询。你可以用低级的API.

试试

有一个解决方法:制作您自己的 Cursor class。它应该由低级 Cursor 和一个偏移量组成。显式设置 chunk() 大小,然后您的游标应由索引 0 处的 Cursor 加上块中的偏移量组成。

然后当您想在该游标处重新启动查询时,请使用 .cursor(batchStartCursor).offset(offsetIntoBatch)