The cheapest way is to use the value you are checking for as a datastore
key, then to do a get by key. A query always results in an index operation
followed by a fetch by key, so you are reducing the need for an index
lookup.

--
Ikai Lan
Developer Programs Engineer, Google App Engine
Blogger: http://googleappengine.blogspot.com
Reddit: http://www.reddit.com/r/appengine
Twitter: http://twitter.com/app_engine



On Tue, Oct 26, 2010 at 10:02 AM, Dan Dubois <uvico...@gmail.com> wrote:

> What is the absolute cheapest way (CPU cost) theoretically to run an
> SQL EXISTS query with the Datastore API?
>
> I imagine FetchOptions would be
> FetchOptions.Builder.withChunkSize(1).limit(1).prefetchSize(1) and
> probably option 1 below.
>
>                Query query = new Query("KIND");
>                //add filters and sorts here, if querying by key use
> query.setKeysOnly()
>                DatastoreService datastoreService =
> DatastoreServiceFactory.getDatastoreService();
>                PreparedQuery pq = datastoreService.prepare(query);
>
>                //Option 1
>                long count =
>
> pq.countEntities(FetchOptions.Builder.withChunkSize(1).limit(1).prefetchSize(1));
>
>                if (count == 0) {
>                        //does not exist
>                }
>                else {
>                        //exists
>                }
>
>                // or maybe
>                //Option 2
>                if (pq.asIterator().hasNext()) {
>                        //exists
>                }
>                else {
>                        //does not exist
>                }
>
> Is there anything cheaper?
>
> Best wishes,
> Dan
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> google-appengine-j...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to