The App Engine Datastore uses indexes for every query your application makes. These indexes are updated whenever an entity changes, so the results can be returned quickly when the app makes a query. To do this, the datastore needs to know in advance which queries the application will make. You specify which indexes your app needs in a configuration file. The development server can generate the datastore index configuration automatically as you test your app.
Creating Datastore indexes
You specify configuration for datastore indexes in
WEB-INF/datastore-indexes.xml, in your
As described on the Datastore Indexes page, an index is a table of values for a set of given properties for entities of a given kind. Each column of property values is sorted either in ascending or descending order. Configuration for an index specifies the kind of the entities, and the names of the properties and their sort orders.
The following is an example of the
datastore-indexes.xml that specifies two indexes:
<?xml version="1.0" encoding="utf-8"?> <datastore-indexes autoGenerate="true"> <datastore-index kind="Employee" ancestor="false"> <property name="lastName" direction="asc" /> <property name="hireDate" direction="desc" /> </datastore-index> <datastore-index kind="Project" ancestor="false"> <property name="dueDate" direction="asc" /> <property name="cost" direction="desc" /> </datastore-index> </datastore-indexes>
Creating indexes using the development server
Determining the indexes required by your application's queries manually can be
tedious and error-prone. Thankfully, the development server can determine the
index configuration for you. To use automatic index configuration, add the
autoGenerate="true" to your
<datastore-indexes> element. Automatic index configuration is also used if
your app does not have a
With automatic index configuration enabled, the development server maintains a
war/ directory. When your app, running in the development server,
attempts a datastore query for which there is no corresponding index in either
datastore-indexes-auto.xml, the server adds the
appropriate configuration to
If automatic index configuration is enabled when you upload your application,
AppCfg uses both
determine which indexes need to be built for your app in production.
autoGenerate="false" is in your
datastore-indexes.xml, the development
AppCfg ignore the contents of
datastore-indexes-auto.xml. If the
app running locally performs a query whose index is not specified in
datastore-indexes.xml, the development server throws an exception, just as the
production Datastore would.
It's a good idea to occasionally move index configuration from
datastore-indexes.xml, then disable automatic
index configuration and test your app in the development server. This makes it
easy to maintain indexes without having to manage two files, and ensures that
your testing will reproduce errors caused by missing index configuration.
Creating indexes manually
You can add indexes to your
datastore-indexes.xml file manually or you might shift
indexes that the development server had automatically created in the
datastore-indexes-auto.xml file into your
datastore-indexes.xml reference for
When you upload an application using the
update action, the update
includes the app's index configuration (the
generated/datastore-indexes-auto.xml files). If the index
configuration defines an index that doesn't exist yet on App Engine, App Engine
creates the new index. Depending on how much data is already in the datastore
that needs to be mentioned in the new index, the process of creating the index
may take a while. If the app performs a query that requires an index that hasn't
finished building yet, the query will raise an exception.
To prevent this, you must ensure that the new version of the app that
requires a new index is not the live version of the application until the
indexes finish building. One way to do this is to give the app a new version
appengine-web.xml whenever you add or change an index in
the configuration. The app is uploaded as a new version, and does not become the
default version automatically. When your indexes have finished building, you can
change the default
version in the
Cloud Platform Console.
Another way to ensure that new indexes are built before the new app goes live is
to upload the index configuration separately before uploading the app. To upload
only the index configuration for an app, use the
./appengine-java-sdk/bin/appcfg.sh update_indexes myapp/war
datastore-indexes.xml file should be in the default module's
In the Cloud Platform Console, you can check the status of the app's indexes.
Deleting unused indexes
When you change or remove an index from the index configuration, the original index is not deleted from App Engine automatically. This gives you the opportunity to leave an older version of the app running while new indexes are being built, or to revert to the older version immediately if a problem is discovered with a newer version.
When you are sure that old indexes are no longer needed, you can delete them
from App Engine using the
./appengine-java-sdk/bin/appcfg.sh vacuum_indexes myapp/war
This command deletes all indexes for the app that are not mentioned in the local