最近,我尝试将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 v5 和 that 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());
}