Fastest way to check if an object exists
Asked Answered
H

4

5

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.

Hanus answered 4/8, 2010 at 19:8 Comment(0)
S
3

Any ideas on how to do this with an index only hit? I was also thinking of a keys only query

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.

Screamer answered 4/8, 2010 at 20:32 Comment(0)
K
4

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.

Kincardine answered 12/3, 2011 at 0:39 Comment(1)
Yeah, you're right, it probably is... still feels very wrong, though. I need to load an entire object + data just to see if that name (key) is already taken :-/Hanus
S
3

Any ideas on how to do this with an index only hit? I was also thinking of a keys only query

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.

Screamer answered 4/8, 2010 at 20:32 Comment(0)
E
1

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!

Edelstein answered 5/8, 2010 at 9:55 Comment(0)
P
1

You can try a keys only query from Objectify like this:

public static boolean objectExists(String id, Class<?> objectClass) {
  return OfyService.ofy().load().filterKey(Key.create(objectClass, id)).keys().first() != null;
}
Pollinosis answered 27/8, 2014 at 17:40 Comment(1)
Should be ...keys().first().now() != null. Otherwise LoadResult<T> is compared against null which gives a different result (see API).Asepsis

© 2022 - 2024 — McMap. All rights reserved.