检查对象是否存在的最快方法
我将 GAE/Java 与 Objectify 结合使用,并且尝试找到最快的方法来检查给定密钥是否存在于数据存储中。我现在正在做的是 .get(key)
和 @Cached
开启,但无论哪种方式仍然检索整个对象,这是不必要的。
关于如何仅命中索引有什么想法吗?我也在考虑仅键查询,但我看到(在 系统状态仪表板 )表明延迟远大于 get
。
I'm using GAE/Java with Objectify, and I'm trying to find the fastest way to check if a given object exists in the datastore, given the key. What I'm doing right now is .get(key)
with @Cached
on, but either way that still retrieves the entire object, which is unnecessary.
Any ideas on how to do this with an index only hit? I was also thinking of a keys only query, but I'm seeing (on the system status dashboard ) that the latency is much more than a get
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在缓存打开的情况下执行 get() 即可。除非 @PostLoad 方法中有很多昂贵的逻辑,否则从 memcache 中获取数据应该比直接访问数据存储区(即使是仅键查询)要便宜得多。缓存是你的朋友。
附带说明一下,这听起来像是过早的优化。使用最方便的代码构建您的应用程序,然后运行 appstats 并找出应用程序中的实际成本。您可能会发现昂贵的部件并不是您想象的那样。
Just perform a get() with the cache turned on. Unless you have a lot of expensive logic in a @PostLoad method, it should be significantly cheaper to fetch the data out of memcache than it is to go all the way to the datastore for even a keys-only query. The cache is your friend.
As a side note, this sounds like premature optimization. Build your app using the most convenient code, then run appstats and find out where the actual costs are in your application. You'll probably find that the expensive parts are not what you think.
仅键查询是获得仅索引命中的唯一方法。它是否比 get 更快取决于实体的大小和索引的大小。在一个简单的示例中,我的获取时间约为 8 毫秒,查询时间约为 13 毫秒。您可以使用 AppStats 来根据真实数据找出哪个对您来说更便宜。
A keys-only query is the only way to get an index-only hit. Whether it's faster than a get depends on the size of your entity and the size of your index. In a trivial example, I'm getting around 8ms for a get and 13ms for a query. You can use AppStats to figure out which is cheaper for you with real data.
在
__key__
上使用过滤器的仅键查询将比在状态仪表板上进行基准测试的查询快得多。我不确定它是否比简单地获取实体更快 - 尝试一下并让我们知道!A keys only query with a filter on
__key__
will be substantially faster than the queries benchmarked on the status dashboard. Whether or not it's faster than simply fetching the entity I'm not sure - try it and let us know!您可以尝试从 Objectify 进行仅键查询,如下所示:
You can try a keys only query from Objectify like this: