Retrieve TOP 1 on Google App Engine
How c开发者_如何学Can I retreive only one entity from a query on Google App Engine. I'm creating a query for with ORDER BY and using:
List<Object> objs= (List<Object>) pm.newQuery(query).execute();
if (objs.size() > 0) {
obj= objs.get(0);
}
But, when I have a large amount of "Objs" in Database, the query is too expensive, and I'm having too much delay.
There is another way to do this query?
Have a look at this document.
These are valid usages of a query:
// Construct then prepare your query
List<Entity> get5TallestPeople() {
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Person");
q.addSort("height", SortDirection.DESCENDING);
PreparedQuery pq = ds.prepare(q);
return pq.asList(FetchOptions.Builder.withLimit(5));
}
and
return pq.asList(FetchOptions.Builder.withLimit(5).offset(5)));
In your case I guess you can use the latter syntax.
It uses low level Java API rather than JDO, but it shouldn't put you off from using it.
have you tried using the fetch
method.. this can give you a similar result as top 1
精彩评论