The results of a JDOQL query can be sorted based on a property and a direction,
either ascending or descending. If a sort order is not specified, then the results of the
query are returned in the order of their entity keys.
As with filters, there are some restrictions on the sorting that can be performed. If
your query includes sort orders on some properties and inequality filters on other
properties, then the property that includes the inequality filters must be ordered
before the other properties. Here is a short example of sorting by multiple properties.
qry.setOrdering("dateCreated desc, stateName asc");
Your JDOQL queries can specify a range of entities to be returned to your application.
The setRange method accepts numeric indexes for the first and last entities that should
be included in the resultset. The index is zero-based, so given the query below, the third,
fourth, and fifth entities will be returned in the results.
Using ranges can be resource-intensive because the datastore returns all entities
and then discards the ones prior to the starting index. Use ranges with care for large
You might be tempted to use ranges to implement pagination for your
application. However, App Engine recommends a slightly different approach, as
discussed in the article, “Paging through large datasets”
For performance and scalability, the datastore maintains an index for each query that
your application can execute. An index is built as a combination of each kind, filter
property and operator, and sort order for every query in your application. As changes
are made to your entities in the datastore, the datastore automatically updates its
indexes with the correct results. When a JDOQL query is executed, the datastore
returns the results directly from its corresponding index.
Scanning Google Groups you will find that there is much uncertainty surrounding
indexes and how they are built. You can either define them manually in the
datastore-index.xml configuration file, or the development web server may create
them for you.