使用Objectify v6进行慢速筛选键查询

时间:2019-01-11 07:32:46

标签: google-cloud-datastore objectify

最近,我尝试将Objectify从v5升级到v6,并遇到了有关过滤键查询的性能问题。

我要做的是一个简单的范围键查询,即类似

ofy()
    .load()
    .type(NotificationEntity.class)
    .filterKey(">=", Key.create(NotificationEntity.class, recipient.id()))
    .filterKey("<", Key.create(NotificationEntity.class, recipient.id() + "\ufffd"))
    .keys()
    .list()

这是Stackdriver Trace of Objectify v5that of v6 。 如这些跟踪所示,v6花了10倍的时间来处理密钥查询。 有人知道幕后发生了什么吗?

这是我用来发出跟踪的代码:

final List<Key<NotificationEntity>> keys;
try (final Scope ss = TRACER.spanBuilder("DatastoreClient.getNotifications.loadKeys").startScopedSpan()) {
  keys = ofy().load().type(NotificationEntity.class)
      .filterKey(">=", Key.create(NotificationEntity.class, recipient.id()))
      .filterKey("<", Key.create(NotificationEntity.class, recipient.id() + "\ufffd"))
      .keys()
      .list();
}
final Map<Key<NotificationEntity>, NotificationEntity> entities;
try (final Scope ss = TRACER.spanBuilder("DatastoreClient.getNotifications.loadEntities").startScopedSpan()) {
  entities = ofy().load().keys(keys);
}
try (final Scope ss = TRACER.spanBuilder("DatastoreClient.getNotifications.sortEntities").startScopedSpan()) {
  return entities
      .values()
      .stream()
      .sorted(Comparator.comparing(NotificationEntity::getTimestamp).reversed())
      .collect(toImmutableList());
}

0 个答案:

没有答案